我创建了一个 UIView (它只是一个蓝色矩形),我希望它粘在屏幕框架/ View 的底部。
var nextBar = UIView()
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: 600, width: self.view.frame.width, height: 50)
self.view.addSubview(nextBar)
我假设我需要在 CGRect 中的“y:”部分之后添加一些内容,但我似乎无法使其工作或找到任何可以快速显示这一点的地方。
提前致谢
最佳答案
嗯,试试这个...
override func viewDidLoad() {
super.viewDidLoad()
let theHeight = view.frame.size.height //grabs the height of your view
var nextBar = UIView()
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: theHeight - 50 , width: self.view.frame.width, height: 50)
self.view.addSubview(nextBar)
}
对于 CRect 中的“y:”,您将获取屏幕尺寸的高度大小,并减去所需的像素数。无论屏幕尺寸如何,它的显示效果都是相同的。
关于ios - 在 Swift 中使 UIView 粘在 ViewController 框架的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30767220/