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

标签 ios swift autolayout screen-orientation

我有一个 ViewController 类,它有一个 textField 变量,该变量已配置 当它在 ViewControllerviewDidLoad 方法中设置时。

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

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

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

更改toItem属性:

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 来说似乎有点笨拙。我是否错过了自动布局的技巧?

最佳答案

实际上你代码中的问题是你没有将textField的translatesAutoresizingMaskIntoConstraints设置为false, 每当你想使用自动布局约束时,你必须将 View 的 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()

    }
}

关于ios - 根据方向更新自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390240/

相关文章:

ios - 内容拥抱优先级与自动布局的行为不同

ios - 用户特定数据在登录 firebase 时被删除

ios - 在没有 UILongPressGestureRecognizer 的情况下快速识别长按

ios - TestFlight Beta 测试 - 我们能否为不同的 1000 名外部测试人员提供同一应用程序的多个版本

ios - SKAction 不重启

ios - 如何让框架之外实现一些特定的方法

ios - 跨多个 UICollectionViewControllers 重用 UICollectionView

ios - 如何在 facebook 应用程序的顶部栏等 UIButton 文本下方添加一行。?

ios - 根据不同的设备设置UITextView字体大小

iphone - 在视网膜显示器上使用实际像素绘图