Xcode 10.3,
In iPhone 8 12.4 Simulator ist es OK.
In iPhone X 12.4 Simulator habe ich alles oben versucht, nicht OK.
Dann füge ich es von Hand hinzu, Statusleiste besteht aus Zeit, Batterie, Handy.
StatusBarView
class StatusBottomView: UIView {
private var batteryView:BatteryView!
private var timeLabel:UILabel!
private var timer:Timer?
override init(frame: CGRect) {
super.init(frame: frame)
addSubviews()
}
private func addSubviews() {
batteryView = BatteryView()
batteryView.tintColor = UIColor.blue
addSubview(batteryView)
timeLabel = UILabel()
timeLabel.textAlignment = .center
timeLabel.font = UIFont.systemFont(ofSize: 12)
timeLabel.textColor = UIColor.blue
addSubview(timeLabel)
didChangeTime()
addTimer()
}
override func layoutSubviews() {
super.layoutSubviews()
let h = frame.size.height
let x: CGFloat = 80
batteryView.frame.origin = CGPoint(x: ScreenHeight - x - DZMBatterySize.width, y: (h - DZMBatterySize.height) / 2)
timeLabel.frame = CGRect(x: x, y: 0, width: 50, height: h)
}
func addTimer() {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(didChangeTime), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: .common)
}
}
func removeTimer() {
if timer != nil {
timer!.invalidate()
timer = nil
}
}
@objc func didChangeTime() {
timeLabel.text = TimerString("HH:mm")
batteryView.batteryLevel = UIDevice.current.batteryLevel
}
}
BatteryLevelView
class BatteryView: UIImageView {
override var tintColor: UIColor! {
didSet{ batteryLevelView.backgroundColor = tintColor }
}
var batteryLevel:Float = 0 {
didSet{ setNeedsLayout() }
}
private var batteryLevelView:UIView!
convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: DZMBatterySize.width, height: DZMBatterySize.height))
}
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: DZMBatterySize.width, height: DZMBatterySize.height))
addSubviews()
}
func addSubviews() {
batteryLevelView = UIView()
batteryLevelView.layer.masksToBounds = true
addSubview(batteryLevelView)
image = UIImage(named: "battery_black")?.withRenderingMode(.alwaysTemplate)
tintColor = UIColor.white
}
override func layoutSubviews() {
super.layoutSubviews()
let spaceW:CGFloat = 1 * (frame.width / DZMBatterySize.width) * HJBatteryLevelViewScale
let spaceH:CGFloat = 1 * (frame.height / DZMBatterySize.height) * HJBatteryLevelViewScale
let batteryLevelViewY:CGFloat = 2.1*spaceH
let batteryLevelViewX:CGFloat = 1.4*spaceW
let batteryLevelViewH:CGFloat = frame.height - 3.4*spaceH
let batteryLevelViewW:CGFloat = frame.width * HJBatteryLevelViewScale
let batteryLevelViewWScale:CGFloat = batteryLevelViewW / 100
var tempBatteryLevel = batteryLevel
if batteryLevel < 0 {
tempBatteryLevel = 0
}else if batteryLevel > 1 {
tempBatteryLevel = 1
}
batteryLevelView.frame = CGRect(x: batteryLevelViewX , y: batteryLevelViewY, width: CGFloat(tempBatteryLevel * 100) * batteryLevelViewWScale, height: batteryLevelViewH)
batteryLevelView.layer.cornerRadius = batteryLevelViewH * 0.125
}
}