Home > Net >  How to update the constant height constraint of a UIView with IBOutlet
How to update the constant height constraint of a UIView with IBOutlet

Time:11-11

I'm trying to change the height of my menu view so that it opens, but when I add the IBOutlet of the NSLayoutConstraint it keeps crashing my app with the same error code. I Triple checked that the Outlet is correctly connected so that's not the problem. Am I missing something or is this updated within the new Xcode as all other questions are from at least 5 years ago.

thanks!

error message:

Thread 1: "[<UIViewController 0x7f8fe2705330> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key menuHeightConstraint."

Relevant code:

class HomeViewController: UIViewController {
    
    // MARK: IBOutlets
    
    @IBOutlet weak var menuHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var menuView: UIView!
    
    
    // MARK: Variables
    
    var menuIsOpen = false
    
    // MARK: IBaction functions
    
    @IBAction func openMenu() {
        menuIsOpen.toggle()
        menuHeightConstraint.constant = menuIsOpen ? 180 : 55
        
        UIView.animate(
            withDuration: 1,
            delay: 0,
            options: .curveEaseIn,
            animations: { self.menuView.layoutIfNeeded() },
            completion: nil)
    }
    
}

IBOutlet in storryboard ---- Hierarchy in storyboard ----

CodePudding user response:

It's unclear if your menuView outlet is hooked up, but from the screenshot you included it seems it isn't. In that case it'd be crashing because menuView will be nil when you try to call self.menuView.layoutIfNeeded(). Either way, you should be calling self.view.layoutIfNeeded() instead anyway.

CodePudding user response:

It seemed to be an Xcode error, I restarted the project the next day and it worked fine. So if you want to edit a constraint with animation, you can do it as seen above :-)

  • Related