ios - 在看到 View 之前添加约束?

标签 ios swift autolayout uikit nslayoutconstraint

我正在尝试以编程方式使用约束将带有 width = 40 的箭头按钮居中。

如果我在 viewDidAppear 中调用它,我的代码可以正常工作,但在 viewDidLoad 中崩溃。

将它放在 viewDidAppear 中是一种眼睛疼痛,因为您看到屏幕然后看到按钮跳到屏幕中间。知道如何设置它在显示 View 之前?

我可能会补充说这是一个 subview ,所以我从一个 viewController 调用 centerArrow(),它的 View 不是同一个 View 。

所以在 viewController 中:

let pview = NSBundle.mainBundle().loadNibNamed("Profile", owner: nil, options: nil)[0] as! ProfileView


override func viewDidAppear(animated: Bool) {
    pview.centerArrow()
}

在继承UIViewProfileView中:

func centerArrow()
{
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let width = (screenSize.width / 2) - 20
    let constraint = NSLayoutConstraint(item: arrowButton, attribute: NSLayoutAttribute.LeftMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.LeftMargin, multiplier: 1, constant: width)
    self.addConstraint(constraint)
}

最佳答案

尝试使用 viewDidLayoutSubViews,这可能会提供您在这种情况下寻找的时间。

关于ios - 在看到 View 之前添加约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677038/

相关文章:

sql-server - 从 native iOS 应用程序查询 SQL Server 数据库

swift - 使用 PickerViewer 从 Firestore 过滤数据后,我无法将数据发送到另一个 VC

ios - 获取 NSError 域 : "com.google.GTLJSONRPCErrorDomain" while fetching Google Calendar events in Swift

ios - 带有 AutoLayout 的 UITextField 边框闪烁

ios - 在容器 View 中嵌入多个 View Controller

ios - Monotouch - UIViewController 内的 TableView : override functions don't get called

ios - Swift:UIScrollView 根本无法使用约束?

ios - View 位于标签栏下 - iOS7 Autolayout

iOS AutoLayout - 什么时候运行?

iphone - 如何使用 Flash 为 iphone 5 优化我的 AIR iOS 应用程序