约束常量更新在iOS 8上不起作用

7 xcode constraints ios autolayout ios8

我对这个简单的UIViewController的约束有一个奇怪的问题.我只有两个元素,正是:

1 UIButton有这些约束: 按钮约束 1具有这些约束的PageControl: PageControl约束 问题出在iOS 8中.我已经连接了UIPageControl的BottomSpace约束来改变常量值(在iOS 7中完美地工作).在iOS 8中,该值不会发生任何错误.我的代码很简单:

pageControlBottomConstraint.constant = 50
Run Code Online (Sandbox Code Playgroud)

两个iOS版本有什么区别?

art*_*mal 8

请执行下列操作,

1.调用updateConstraintsIfNeeded
2.调用layoutIfNeeded(对于currentView和ViewYouWantToUpdate)

所以你的代码应该是

[pageControl updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
[pageControl layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)

如果你想动画 Constraints

Objective-C的

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

Swift 3.0:

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