在 Swift 中将 UIView 固定在 ViewController 框架的底部
Make a UIView stick to bottom of the ViewController frame in Swift
我创建了一个 UIView(它只是一个蓝色矩形),我希望它贴在屏幕 frame/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:' 部分之后添加一些东西,但我似乎无法让它工作,也无法在 swift 中找到显示此内容的任何地方.
提前致谢
嗯嗯试试这个...
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:',您将采用屏幕尺寸的高度尺寸,然后减去所需的像素数。对于任何屏幕尺寸,它看起来都是一样的。
您可以使用约束以编程方式完成此操作
fileprivate func setupName(){
let height = CGFloat(50)
lblName.text = "Hello world"
lblName.backgroundColor = .lightGray
//Step 1
lblName.translatesAutoresizingMaskIntoConstraints = false
//Step 2
self.view.addSubview(lblName)
//Step 3
NSLayoutConstraint.activate([
lblName.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
lblName.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
lblName.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -height),
lblName.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])
}
详情请参考link
像这样:
view.addConstraintsWithFormat(format: "V:[v0(50)]|", views: nextBar)
我创建了一个 UIView(它只是一个蓝色矩形),我希望它贴在屏幕 frame/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:' 部分之后添加一些东西,但我似乎无法让它工作,也无法在 swift 中找到显示此内容的任何地方.
提前致谢
嗯嗯试试这个...
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:',您将采用屏幕尺寸的高度尺寸,然后减去所需的像素数。对于任何屏幕尺寸,它看起来都是一样的。
您可以使用约束以编程方式完成此操作
fileprivate func setupName(){
let height = CGFloat(50)
lblName.text = "Hello world"
lblName.backgroundColor = .lightGray
//Step 1
lblName.translatesAutoresizingMaskIntoConstraints = false
//Step 2
self.view.addSubview(lblName)
//Step 3
NSLayoutConstraint.activate([
lblName.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
lblName.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
lblName.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -height),
lblName.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])
}
详情请参考link
像这样:
view.addConstraintsWithFormat(format: "V:[v0(50)]|", views: nextBar)