ios - Swift 中的尾随和前导约束以编程方式 (NSLayoutConstraints)

标签 ios iphone swift2 nslayoutconstraint

我正在从 xib 添加一个 View 到我的 ViewController 中。然后我将它的约束设置为实际适合它

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    ...
    ...
    view!.addSubview(gamePreview)
    gamePreview.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 9.0, *) {
        // Pin the leading edge of myView to the margin's leading edge
        gamePreview.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
        //Pin the trailing edge of myView to the margin's trailing edge
        gamePreview.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true

    } else {
        // Fallback on earlier versions
        view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .TrailingMargin, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1, constant: 0))

        view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .LeadingMargin, relatedBy: .Equal, toItem: view, attribute: .LeadingMargin, multiplier: 1, constant: 0))
    }
    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,multiplier: 1, constant: 131))
}

我正在尝试做的事情:为了实际适合我的 View ,将它限制在顶部、前导、尾随到 ViewController 的 View ,并具有前缀高度。我添加到主视图的 View 有它自己的透明背景 View ,所以不需要边距( View 意味着设备的宽度大小,所以)。

我已经放置了 2 对应该相等的行(在我的尝试中),与 if,因为 if 中的前两行实际上仅在 iOS9> 中可用,而我正在尝试这样做对于每台设备(从 iOS 8 开始),else 语句中的内容相同。

这是我得到的:

左边是 iOS9+,右边是 iOS8+。透明背景被涂成红色以显示发生的情况(不要介意图像中的不同高度,它们在应用程序中的高度相同,而是查看左右添加的边距)

我也尝试过 AutoLayoutDSL-Swift,但没有帮助,我不是这方面的专家,但每次尝试只会让事情变得更糟。

如何使用经典的 NSLayoutConstraints 方法编写这些约束,以及如何使用像 AutoLayoutDSL 或它的分支这样的框架以更好的方式编写所有内容? (或替代方案,尽管现在我主要关注官方库)

最佳答案

您需要使用 leadingtrailing 属性,而不是 leadingMargintrailingMargin 属性:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    ...
    ...
    view!.addSubview(gamePreview)
    gamePreview.translatesAutoresizingMaskIntoConstraints = false

    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0))            
    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0))

    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: gamePreview, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,multiplier: 1, constant: 131))
}

关于ios - Swift 中的尾随和前导约束以编程方式 (NSLayoutConstraints),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733551/

相关文章:

ios - 按钮位置相应变化 - 为 iPhone 5 定制

iphone - 将字符串发送到 iOS 中的网络服务器?

iphone - Xcode 的 "build and analyze"报告此代码存在奇怪的内存泄漏

swift - 为什么 max 在 Swift 的 CGFloat 扩展中不起作用?

ios - 更新到 iOS 版本 9.3.3 后,重定向 url 以启动应用程序不起作用

swift - 在通用元类型函数中返回具有特定元类型的对象

android - 在 flutter 中仅显示图像的顶部部分

ios - 如何将 NSString 转换为 int

iphone - 如何将 Admob 横幅放在 iPhone 4S 和 iPhone 5 的底部?

iphone - 如何使用 Oauth 将照片上传到 Plixi (Tweetphoto)?