xcode 约束不断更新不适用于 iOS 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26354880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:52:08  来源:igfitidea点击:

Constraint constant update not work on iOS 8

iosxcodeios8autolayoutconstraints

提问by arthankamal

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

我对这个简单的 UIViewController 的约束有一个奇怪的问题。我只有两个元素,准确地说:

1 UIButton with these constraints: Button Constraints1 PageControl with these constraints: PageControl ConstraintsThe 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:

1 个带有这些约束的 UIButton: 按钮约束1 个带有这些约束的 PageControl: 页面控件约束问题出在 iOS 8 中。我已经连接了 UIPageControl 的底部空间约束来更改常量值(在 iOS 7 中完美运行)。在 iOS 8 中,该值不会在没有任何错误的情况下更改。我的代码很简单:

pageControlBottomConstraint.constant = 50

What is the difference between the two iOS version?

两个iOS版本有什么区别?

回答by arthankamal

Do the following,

请执行下列操作,

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

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

So you code should be

所以你的代码应该是

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

if you want to animate the Constraints

如果你想动画 Constraints

Objective-C

目标-C

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

Swift 3.0:

斯威夫特 3.0:

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