Remove constraints iOS

你离开我真会死。 提交于 2020-01-16 09:04:41

问题


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.

This function setup Portrait mode.

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

}

This function setup landscape mode.

    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
}

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


回答1:


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

Instead, you need to say something like

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.




回答2:


You can remove your constraints using:

myView.constraints.removeAll()

Before setting the new ones.



来源:https://stackoverflow.com/questions/45944479/remove-constraints-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!