Update storyboard constraint using code

前端 未结 1 507
青春惊慌失措
青春惊慌失措 2021-01-24 15:07

I have a UIImage in code that I would like to enlarge vertically when a button is pressed. The UIImage is fully constrained in the storyboard and I wou

1条回答
  •  心在旅途
    2021-01-24 15:33

    You should to call layoutIfNeeded within the animation block. Apple actually recommends you call it once before the animation block to ensure that all pending layout operations have been completed. I just checked it with the resizing of button - everything works fine.

    @IBOutlet weak var myBtn: UIButton!
    @IBOutlet weak var btnHeight: NSLayoutConstraint!
    @IBOutlet weak var btnWidth: NSLayoutConstraint!
    
    
    @IBAction func resizeBtn(sender: AnyObject) {
        self.myBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
    
        self.view.layoutIfNeeded()
        self.btnHeight.constant += 50
        self.btnWidth.constant += 50
    
        UIView.animateWithDuration(0.7, animations: {
            self.view.layoutIfNeeded()
        })
    }
    

    P.S. make sure that other constraints don't block your changes.

    0 讨论(0)
提交回复
热议问题