ios - 在 Swift 中使 UIView 粘在 ViewController 框架的底部

标签 ios swift uiview cgrect

我创建了一个 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/

相关文章:

iphone - iOS 应用程序在线程 0 中崩溃

ios - CoreData OneToMany 错误

ios - 如何解决 Swift 3 中的自动布局问题?

ios - 一旦已经在 WebView 中,如何使用 WKWebView 打开外部链接?

ios - 无法从黑色更改 UIView 背景颜色

swift - 减少 swift UIView 中的冗余

php - 上传一个sqlite文件

html - 用于视网膜显示的 uiwebview 内容的 iOS 大小

ios - 如何修复 C 风格的语句?

ios - 获取另一个 UIView 下的 UIView