Search code examples
iosxcodeios8autolayoutconstraints

Constraint constant update not work on iOS 8


I have a strange problem with my constraints on this simple UIViewController. I have only two elements in it and precisely:

1 UIButton with these constraints: Button Constraints 1 PageControl with these constraints: PageControl Constraints The problem is in iOS 8. I have connected the BottomSpace Constraint of UIPageControl to change the constant value (that in iOS 7 works perfectly). In iOS 8 the value doesn't change without any error. My code is simple:

pageControlBottomConstraint.constant = 50

What is the difference between the two iOS version?


Solution

  • Do the following,

    1. call updateConstraintsIfNeeded
    2. call layoutIfNeeded (for currentView and ViewYouWantToUpdate)

    So you code should be

    [pageControl updateConstraintsIfNeeded];
    [self.view layoutIfNeeded];
    [pageControl layoutIfNeeded];
    

    if you want to animate the Constraints

    Objective-C

    [pageControl updateConstraintsIfNeeded]; 
    [UIView animateWithDuration:.75f animations:^{
         __weak __typeof__(self) weakSelf = self
         [self.view layoutIfNeeded];
         [pageControl layoutIfNeeded];
    }
    

    Swift 3.0:

    pageControl.updateConstraintsIfNeeded()
    UIView.animate(withDuration: 0.75) {
         weak var weakSelf = self
         weakSelf?.view.layoutIfNeeded()
         weakSelf?.pageControl.layoutIfNeeded()
    }