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

时间:2016-04-03 19:41:15

标签: ios swift autolayout screen-orientation

我有一个ViewController类,它有一个textField变量,已配置 当它在viewDidLoad的{​​{1}}方法中设置时。

ViewController

一组自动布局约束被添加到视图中。我想要这个文本字段 以中间(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)为中心,宽度(高度)为80%(5%) 在人像模式下计算的.CenterY&n; ViewController

如果将设备的方向(view)旋转到 横向方向,或从横向方向呈现,我想要宽度(高度) 与纵向方向保持一致。

更改UIDevice.currentDevice().orientation的{​​{1}}:

attribute

为:

toItem

没有做到这一点,因为我猜在设备切换方向时view.addConstraint(NSLayoutConstraint(item: textField, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 0.05, constant: 0)) 没有得到(重新)设置。 此外,对于Swift来说,这个检查似乎有些笨拙。我错过了自动布局的技巧吗?

2 个答案:

答案 0 :(得分:2)

实际上代码中的问题是您没有将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()

    }
}

答案 1 :(得分:0)

一种解决方案是获取屏幕的固定坐标范围(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)
            ])
    }
}

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