根据方向更新自动布局约束

Updating autolayout constraints depending on orientation

我有一个 ViewController class 有一个 textField 变量,它被配置 在 ViewController.

viewDidLoad 方法中设置时
class ViewController: UIViewController {

    weak var textField: UITextField! {
        didSet {
            textField.delegate = self
            textField.placeholder = "Enter Text"
            view.addSubview(textField)
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 0.80, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 0.05, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        textField = UITextField()
    }
}

视图中添加了一组自动布局约束。我想要这个文本字段 居中于中间(.CenterX.CenterY),为宽度(高度)的 80% (5%) ViewControllerview 纵向 模式下计算。

如果设备的方向 (UIDevice.currentDevice().orientation) 旋转到 横向,或者从横向呈现,我想要宽度(高度) 保持与纵向相同。

正在为 toItem 中的 attribute 更改:

view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 0.05, constant: 0))

至:

attribute: (UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation) ? .Height : .Width)

没有成功,因为我猜 textField 在设备切换方向时没有得到(重新)设置。 此外,此检查对于 Swift 似乎有点笨拙。我是不是错过了自动布局的技巧?

一个解决方案是获取屏幕的固定坐标范围 (iOS >= 8) 并适当调整前两个约束:

class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let textField = UITextField()
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.delegate = self
        textField.placeholder = "Enter Text"
        view.addSubview(textField)

        textField.backgroundColor = UIColor.redColor()
        textField.textAlignment = .Center

        let fixedScreenBounds = UIScreen.mainScreen().fixedCoordinateSpace.bounds

        NSLayoutConstraint.activateConstraints([
            NSLayoutConstraint(item: textField, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 0.8*fixedScreenBounds.width),
            NSLayoutConstraint(item: textField, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 0.05*fixedScreenBounds.height),
            NSLayoutConstraint(item: textField, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: textField, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
            ])
    }
}

另外,请注意,由于您是在代码中创建文本字段并且想要使用自动布局,因此必须将其 translatesAutoresizingMaskIntoConstraints 设置为 false。

实际上你代码中的问题是你没有将textField的translatesAutoresizingMaskIntoConstraints设置为false, 每当你想使用自动布局约束时,你必须将视图的 translatesAutoresizingMaskIntoConstraints 设置为 false.

我已经更新了你的代码看看它工作正常。

class ViewController: UIViewController, UITextFieldDelegate {    
    weak var textField: UITextField! {
        didSet {
            textField.delegate = self
            textField.placeholder = "Enter Text"
            view.addSubview(textField)
            // to see the size of textField
            textField.backgroundColor = UIColor.purpleColor()

            textField.translatesAutoresizingMaskIntoConstraints = false

            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 0.80, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 0.05, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0))
            view.addConstraint(NSLayoutConstraint(item: textField, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        textField = UITextField()

    }
}