xcode 在约束更新时委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24800414/
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
Delegate when constraints updated
提问by Kampai
I am facing an issue while using storyboard auto layout with UIScrollView. I am updating a constraint of a UIScrollView, And after than setting content size of scrollview. But UIScrollView constraint take a bit time to update and initially UIScrollView not able to scroll because of more height than a view. I can not change current implementation.
我在使用带有 UIScrollView 的情节提要自动布局时遇到问题。我正在更新 UIScrollView 的约束,然后设置滚动视图的内容大小。但是 UIScrollView 约束需要一些时间来更新,并且最初 UIScrollView 无法滚动,因为高度比视图高。我无法更改当前的实现。
Is there any way, notification or any delegate method to check that constraints are updated? So that i can do further changes.
有什么方法、通知或任何委托方法来检查约束是否已更新?这样我就可以做进一步的改变。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewHieghtConstraint;
self.scrollViewHieghtConstraint.constant = 500;
[self.scrollView updateConstraints];
[self.scrollView setContentSize:CGSizeMake(0, 1000)];
回答by Cemal Eker
You can use - (void)updateViewConstraints
selector of UIViewController
.
您可以使用- (void)updateViewConstraints
的选择UIViewController
。
You can also use - (void)updateConstraints
selector of UIView
if you have extended said view.
如果您扩展了所述视图,您也可以使用- (void)updateConstraints
选择器UIView
。
回答by galrito
You can't call -updateConstraints directly. You have to mark the view's constraints as dirty and iOS will call that method on the next update cycle.
您不能直接调用 -updateConstraints。您必须将视图的约束标记为脏,iOS 将在下一个更新周期调用该方法。
On a UIViewController's implementation, add this when you need the constraint to change:
在 UIViewController 的实现上,当您需要更改约束时添加:
self.scrollViewHieghtConstraint.constant = 500;
[self.view setNeedsLayout];
I'm assuming that UIScrollView is a subview of the UIViewController's view. If it's not, then call -setNeedsLayout on the scrollview's parent view.
我假设 UIScrollView 是 UIViewController 视图的子视图。如果不是,则在滚动视图的父视图上调用 -setNeedsLayout。
And if you need to know when the subviews are already on their right place, you have a callback for UIViewController, called -viewDidLayoutSubviews
如果你需要知道子视图何时已经在正确的位置,你有一个 UIViewController 回调,叫做 -viewDidLayoutSubviews