保持自动版式制约设备旋转工作状态 [英] Keep autolayout constraints active status on device rotation

查看:135
本文介绍了保持自动版式制约设备旋转工作状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当我以编程方式更新我的自动布局的限制,当我旋转屏幕的所有变化都还原。

I noticed that when I update my autolayout constraints programmatically, all changes are reverted when I rotate the screen.

重现此问题:


  • 基本故事板带的UIView和2约束接口:

  • Basic Storyboard interface with UIView and 2 constraints:


  1. 宽度等于superview.width(乘数1)主动

  2. 宽度等于superview.width(乘数1/2)禁用

创建和IBOutlet中链接这些约束条件2

  • Create and link these 2 constraints with IBOutlet

    好像对我的错误。

    你怎么想的?

    截图:

    故事板:

    约束#1:

    在这里输入的形象描述

    约束#2:

    在这里输入的形象描述

    推荐答案

    安装是指尺寸安装类,而不是有效 / 非活动

    Size Classes

    Installed refers to Size Classes installation, not to active/inactive.

    您必须以编程方式创建另一个约束,并激活/禁用的的之一。这是因为你不能改变一个约束的乘数(<一个href=\"http://stackoverflow.com/questions/19593641/can-i-change-multiplier-property-for-nslayoutconstraint\">Can我改变乘数属性NSLayoutConstraint?),也可以用大小鼓捣类(<一个href=\"http://stackoverflow.com/questions/27663249/activateconstraints-and-deactivateconstraints-not-persisting-after-rotation-fo\">activateConstraints:和deactivateConstraints:旋转后没有坚持在IB 创建的约束)

    You must create another constraint programmatically, and activate/deactivate that one. This is because you cannot change the multiplier of a constraint (Can i change multiplier property for NSLayoutConstraint?), nor can you tinker with Size Classes (activateConstraints: and deactivateConstraints: not persisting after rotation for constraints created in IB).

    有几个方法可以做到这一点。在下面的例子中,我创建您的 X1 约束的副本,具有事半功倍的或 1/2 。然后,我在两者之间切换:

    There are a few ways to do so. In the example below, I create a copy of your x1 constraint, with a multiplier or 1/2. I then toggle between the two:

    @IBOutlet var fullWidthConstraint: NSLayoutConstraint!
    var halfWidthConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        halfWidthConstraint = NSLayoutConstraint(item: fullWidthConstraint.firstItem,
            attribute: fullWidthConstraint.firstAttribute,
            relatedBy: fullWidthConstraint.relation,
            toItem: fullWidthConstraint.secondItem,
            attribute: fullWidthConstraint.secondAttribute,
            multiplier: 0.5,
            constant: fullWidthConstraint.constant)
        halfWidthConstraint.priority = fullWidthConstraint.priority
    }
    
    @IBAction func changeConstraintAction(sender: UISwitch) {
        if sender.on {
            NSLayoutConstraint.deactivateConstraints([fullWidthConstraint])
            NSLayoutConstraint.activateConstraints([halfWidthConstraint])
        } else {
            NSLayoutConstraint.deactivateConstraints([halfWidthConstraint])
            NSLayoutConstraint.activateConstraints([fullWidthConstraint])
        }
    }
    


    测试上的的iOS 9 + X code 7 +

    这篇关于保持自动版式制约设备旋转工作状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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