【发布时间】:2015-06-08 10:11:30
【问题描述】:
我正在尝试构建一个可重用的UIView,其宽度应等于其在 Swift 中的父视图。
由于其父视图的大小不同,我想我必须通过自动布局为它设置约束。
但我不知道如何在 Swift 中以编程方式进行操作。
这是可重用子视图的代码:
import UIKit
class bottomMenu: UIView {
@IBOutlet var bottomMenu: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
NSBundle.mainBundle().loadNibNamed("bottomMenu", owner: self, options: nil)
bottomMenu.backgroundColor = UIColor.redColor()
//How to make the width of the bottom Menu equal to its superview?
self.addSubview(self.bottomMenu)
}
}
谁能教我怎么做?
谢谢
【问题讨论】:
-
你有自定义 UI 的 xib 吗?
-
是的,我有一个 xib。我已经将宽度设置为 320px。所以当我在 iPhone 5 上运行它时它可以正常工作。但是当我将设备更改为 iPhone 6 时,它无法匹配屏幕的宽度。这就是为什么我希望它可以与自动布局一起使用。
标签: ios swift uiview autolayout constraints