Swift中的尾随和前导约束以编程方式(NSLayoutConstraints) [英] Trailing and Leading constraints in Swift programmatically (NSLayoutConstraints)

查看:363
本文介绍了Swift中的尾随和前导约束以编程方式(NSLayoutConstraints)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从xib中添加一个视图到我的ViewController中.然后,我把它的约束条件真正适合它

I'm adding from a xib a view into my ViewController. Then I'm putting its constraints to actually fit it

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))
}

我正在尝试做的事情:为了真正适合我的视图,将其限制在顶部,顶部,尾随ViewController的视图并带有前缀高度.我要添加到主视图的视图具有其自己的透明背景视图,因此不需要空白(该视图本来就是设备的宽度大小).

What i'm trying to do: To actually fit my view constraining it to top, leading, trailing to ViewController's view, and with a prefixed height. The view I'm adding to main view has its own transparent-background view, so no need of margin (the view is meant to be device's width size, so).

我已经放置了两对本应相等的行(在我的尝试中),使用了if,因为if的前两行仅在iOS9中实际可用,而我正在尝试这样做对于每台设备(从iOS 8开始),else语句中都是相同的.

I've placed 2 couples of lines that would be supposed to be equal (in my attempts), with the if, because the first 2 lines in if are actually available in iOS9> only, while I'm attempting to do the same thing in the else statement, for every device (starting from iOS 8).

这就是我得到的:

左侧为iOS9 +,右侧为iOS8 +.透明背景被涂成红色,以显示发生了什么(不要介意图像中的高度不同,它们在应用程序中的高度相同,而请注意左侧和右侧的附加边距)

iOS9+ at left, iOS8+ at right. Transparent background was colored red to show what happens (don't mind at different height in the images, they're equal height in app, instead look at added margins at left and right)

我也尝试过AutoLayoutDSL-Swift,但没有帮助,我不是专家,但每次尝试只会使情况变得更糟.

I also tried AutoLayoutDSL-Swift, but doesn't help, I'm not expert with it but every attempt made only things worse.

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

How can I write those constraints using classic NSLayoutConstraints methods, and how could I write all in a better way with a framework like AutoLayoutDSL or a fork of it? (or an alternative, though now mostly I'm concerned on official libs)

推荐答案

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

You need to use the leading and trailing attributes, not the leadingMargin and trailingMargin attributes:

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))
}

这篇关于Swift中的尾随和前导约束以编程方式(NSLayoutConstraints)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆