Nic*_*k89 2 uiview ios cgrect swift
我创建了一个UIView(它只是一个蓝色矩形),我希望它能粘在屏幕框架/视图的底部.
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)
Run Code Online (Sandbox Code Playgroud)
我假设我需要在CGRect中的'y:'部分之后添加一些内容,但我似乎无法使其工作或找到任何能够在swift中向我显示此内容的内容.
提前致谢
whe*_*leo 16
嗯试试这个......
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)
}
Run Code Online (Sandbox Code Playgroud)
对于CRect中的'y:',你需要考虑屏幕尺寸的大小,并且你要减去你想要的像素数.对于任何屏幕尺寸,它看起来都是一样的.
小智 5
您可以使用约束以编程方式进行操作
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),
])
}
Run Code Online (Sandbox Code Playgroud)
请参考链接了解更多详情
| 归档时间: |
|
| 查看次数: |
11274 次 |
| 最近记录: |