heightAnchor.constraint not change height of view

人盡茶涼 提交于 2020-07-09 16:25:47

问题


I use this code:

func show(){
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    view.heightAnchor.constraint(equalToConstant: view.frame.height - 300).isActive = true
    view.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
}

func hide(){
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    view.heightAnchor.constraint(equalToConstant: view.frame.height + 300).isActive = true
    view.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
}

show function works fine, but hide function does not work and heightAnchor does not work.


回答1:


First declare constraints

var heightArchonWhenShow = view.heightAnchor.constraint(equalToConstant: view.frame.height - 300)
var heightArchonWhenHide = view.heightAnchor.constraint(equalToConstant: view.frame.height + 300)

After init your constraints on ViewDidLoad

func setConstraints(){
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    view.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
    heightArchonWhenShow.isActive = true
}

And simple active them , don't overwrite. Like this:

func show(){
    heightArchonWhenShow.isActive = true
    heightArchonWhenHide.isActive = false
}

func hide(){
    heightArchonWhenShow.isActive = false
    heightArchonWhenHide.isActive = true
}

If u want u can also animate changes, like that :

func hide(){
    heightArchonWhenShow.isActive = false
    heightArchonWhenHide.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}



回答2:


You need only 1 var

var heightCon = view.heightAnchor.constraint(equalToConstant: view.frame.height - 300)
heightCon.isActive = true

Then manage it's constant value

func showOrHide(_ te:Bool){

   heightCon.constant = te ? view.frame.height - 300 : view.frame.height + 300

     UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}



回答3:


I recommend you to setup your constraint in a different function like maybe setup() and store them in some variable that you can modify after, I'll show you an example

var constraint: NSLayoutConstraint?

func setup() {
    constraint = view.heightAnchor.constraint(equalToConstant: view.frame.height)
}

func show() {
    if constraint != nil {
        constraint!.constant = constraint!.constant + 300
    }
}

func hide() {
    if constraint != nil {
        constraint!.constant = constraint!.constant - 300
    }
}


来源:https://stackoverflow.com/questions/53739783/heightanchor-constraint-not-change-height-of-view

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