默認情況下在iPhone X狀態欄看起來是這樣的:如何獲得一個黑色的狀態欄上的iPhone X iOS上的11
但我想實現這一點:
我嘗試將preferredStatusBarStyle
設置爲lightContent
,但它只在將狀態欄後面的背景設置爲黑色後才起作用。
要修復圓角我最終添加了圓角的另一個子視圖。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let roundedView = UIView(
frame: CGRect(
x: 0,
y: UIApplication.shared.statusBarFrame.height,
width: view.frame.width,
height: view.frame.height
)
)
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
roundedView.backgroundColor = .white
view.addSubview(roundedView)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 60))
label.text = "Black statusbar!"
label.textAlignment = .center
roundedView.addSubview(label)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
我想知道這是否是最好的方法..必須有更好的方法來實現這一點。
UPDATE
這是一個可怕的想法,因爲:
- 作爲@Moritz指出,Apple's guidelines明確禁止這樣的:
不要嘛sk或特別注意關鍵顯示功能。不要試圖隱藏設備的圓角,傳感器外殼或 指示燈,通過在屏幕頂部和底部放置黑條來訪問主屏幕。不要使用類似於 括號,邊框,形狀或教學文字的視覺裝飾品來呼籲特殊的 注意這些區域。
圓角在視圖的頂部看起來不錯,但你必須在代碼中添加例外,以確保圓角不會對其他的iPhone中。你需要把它放在你的所有ViewController/Storyboard中。這並不是很好。
視圖底部的圓角將在屏幕截圖中直線顯示,但頂部的角落(手動設置)不會。當用戶分享你的應用程序時,這會很難看。
安全區域已經引起一大堆的麻煩向後兼容,這只是將是燃料在火。 – Annjawn