gpt4 book ai didi

ios - 仅在 iOS 11 中设置顶部安全区域的背景颜色

转载 作者:可可西里 更新时间:2023-11-01 06:13:02 24 4
gpt4 key购买 nike

我是 Swift 的新手。我已经以编程方式设置了我的顶部栏。它适用于除 iOS 版本 11 之外的所有版本。

this is how it looks in iphone 5s version 11

This is how it looks in iphoneX version 11

我想更改 iPhone X 安全区域的背景颜色。目前我刚刚添加了以下代码来隐藏状态栏。

 override var prefersStatusBarHidden: Bool {
return true
}

我使用代码创建了顶部栏:

//Top Bar
let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
topBar.backgroundColor = UIColor.white
topBar.layer.shadowColor = UIColor.gray.cgColor
topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
topBar.layer.shadowOpacity = 1
topBar.layer.masksToBounds = false
topBar.layer.shadowRadius = 8.0;
//ImageView - Back Button
let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
let backBtnImage = UIImage(named: "back_button") as UIImage?
backBtn.setImage(backBtnImage, for: .normal)
backBtn.layer.masksToBounds = true
backBtn.addTarget(self,action:#selector(backButtonClicked),
for:.touchUpInside)
//Label - Title
let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
titleLabel.text = "Favorites"
titleLabel.contentMode = UIViewContentMode.center
//include all in view
topBar.addSubview(titleLabel)
topBar.addSubview(backBtn)

containerView.addSubview(topBar)

有没有一种方法可以在不使用 UINavigationBar 或设置状态栏的情况下做到这一点。

最佳答案

你好,你也可以试试这个,需要添加一个与您在标题 View 中设置的背景相同的 View ,并添加到容器父 View 。请引用下面的代码

//Top Bar


let safeAreaView = UIView(frame:CGrect(x: 0,y: 0, width: width, height: 40))
safeAreaView.backgroundColor = UIColor.white
self.view.addSubview(safeAreaView)

let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
topBar.backgroundColor = UIColor.white
topBar.layer.shadowColor = UIColor.gray.cgColor
topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
topBar.layer.shadowOpacity = 1
topBar.layer.masksToBounds = false
topBar.layer.shadowRadius = 8.0;
//ImageView - Back Button
let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
let backBtnImage = UIImage(named: "back_button") as UIImage?
backBtn.setImage(backBtnImage, for: .normal)
backBtn.layer.masksToBounds = true
backBtn.addTarget(self,action:#selector(backButtonClicked),
for:.touchUpInside)
//Label - Title
let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
titleLabel.text = "Favorites"
titleLabel.contentMode = UIViewContentMode.center
//include all in view
topBar.addSubview(titleLabel)
topBar.addSubview(backBtn)

containerView.addSubview(topBar)

关于ios - 仅在 iOS 11 中设置顶部安全区域的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com