消除约束iOS [英] Remove constraints iOS

查看:134
本文介绍了消除约束iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试消除约束。我想对肖像和风景设置不同的限制。如果将方向更改为纵向,则调用函数 setupConstrainsInPortrait ,反之亦然。我有两个功能。

I try to remove constraints. I want to different constraints on portrait and landscape. If I change the orientation to Portrait I call the function setupConstrainsInPortrait and conversely. I have two functions.

此功能设置人像模式。

func setupConstrainsInPortrait() {

    view.addSubview(myView)

    myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
    myView.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true

    view.addSubview(switchKmM)

    switchKmM.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    switchKmM.topAnchor.constraint(equalTo: myView.bottomAnchor, constant: 10).isActive = true
    switchKmM.heightAnchor.constraint(equalToConstant: 50).isActive = true
    switchKmM.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

    view.addSubview(speedLbl)

    speedLbl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    speedLbl.topAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
    speedLbl.heightAnchor.constraint(equalToConstant: 50).isActive = true
    speedLbl.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

}

此功能设置横向模式。

    func setupConstrainsInLandScape() {
    view.addSubview(myView)

    myView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
    myView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
    myView.widthAnchor.constraint(equalToConstant: 150).isActive = true

    view.addSubview(switchKmM)

    switchKmM.leftAnchor.constraint(equalTo: myView.rightAnchor, constant: 30).isActive = true
    switchKmM.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
    switchKmM.heightAnchor.constraint(equalToConstant: 50).isActive = true
    switchKmM.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}

我使用这些viewWillTransition中的函数

I use these functions in viewWillTransition

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("landscape!")
        view.backgroundColor = .green
        setupConstrainsInLandScape()
        self.viewWillLayoutSubviews()


    }
    else {
        print("portrét")
        view.backgroundColor = .white
        setupConstrainsInPortrait()
        self.viewWillLayoutSubviews()

    }
    super.viewWillTransition(to: size, with: coordinator)
}

问题是,横向模式中的约束(来自肖像功能)没有被删除

The problem is, that constraints(from portrait function) in landscape mode aren't deleted

我希望有人可以帮助我...谢谢

I hope that someone can help me... Thank you

推荐答案

您做的工作不够。您需要保留对所有激活的约束的引用,以便以后可以将其停用。例如,您说的是:

You're not doing enough work. You need to retain references to all the constraints you activate, so that you can deactivate them later. For example, you are saying:

myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
myView.heightAnchor.constraint(equalToConstant: 300).isActive = true
myView.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true

相反,您需要说些类似

self.myConstraints1 = [
    myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
    myView.heightAnchor.constraint(equalToConstant: 300),
    myView.widthAnchor.constraint(equalToConstant: view.frame.size.width),
]

现在激活所有这些约束。

Now activate all those constraints. Proceed the same way throughout.

因此,当由于方向改变而需要取消约束时,您可以引用它们并且可以这样做。

Thus, when the time comes to deactivate constraints because the orientation is changing, you have references to them and can do so.

这篇关于消除约束iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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