Update storyboard constraint using code

允我心安 提交于 2019-12-02 03:02:25

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.

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