【发布时间】:2017-05-09 16:41:50
【问题描述】:
如果设备是 iPhone SE,我想更新约束。
我有一个 xib 视图控制器,我将它的视图添加为子视图。
//get the empty view
let emptyVC = JobsEmptyStateView(nibName: "JobsEmptyStateView", bundle: nil)
let emptyView = emptyVC.view!
emptyView.frame = self.view.frame
self.navigationController?.view.insertSubview(emptyView, belowSubview: (self.navigationController?.navigationBar)!)
在 JobsEmptyStateView 我有
@IBOutlet weak var headerLabelTopLayout: NSLayoutConstraint!
我也尝试将其设置为private var
最后我有一个类来决定设备是否是 iPhoneSE
class func emptyStateHeaderLabelTopLayout() -> CGFloat? {
let device = Device()
if device.isOneOf(smallScreenDevices) {
if Constants.isDevicePortrait()
{
return 200
}
return 200
}
return nil
}
在 JobsEmptyStateView 中:
override func viewDidLoad() {
if let topConstant = Constants.emptyStateHeaderLabelTopLayout()
{
//self.headerLabelTopLayout.isActive = true
print(self.headerLabelTopLayout.constant)
print(topConstant)
self.headerLabelTopLayout.constant = topConstant
print(self.headerLabelTopLayout.constant)
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
print(self.headerLabelTopLayout.constant)
}
}
打印:
70.0
200.0
200.0
70.0
为什么约束没有得到更新?
【问题讨论】:
-
我想知道它是否结合了 (1) 实例化
JobsEmptyStateView的实例(恕我直言,这是一个名称不佳的 UIViewController),(2)处理viewDidLoad,(3)更新weak约束和 then (4) 使用添加了 VC 的 UIView 作为完全不同的视图层次结构中的子视图(显然,一个未命名的视图控制器)。除了更新常量之外,您还想从高层次上做什么?
标签: ios iphone swift autolayout constraints