当约束更新时委托



我面临一个问题,而使用故事板自动布局与UIScrollView。我正在更新一个UIScrollView的约束,然后设置scrollview的内容大小。但UIScrollView约束需要一些时间来更新,最初UIScrollView不能滚动,因为高度比视图高。我不能改变当前的实现。

是否有任何方法,通知或委托方法来检查约束是否更新,以便我可以做进一步的更改?

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewHieghtConstraint;
self.scrollViewHieghtConstraint.constant = 500;
[self.scrollView updateConstraints];
[self.scrollView setContentSize:CGSizeMake(0, 1000)];

您可以使用UIViewController- (void)updateViewConstraints选择器。

您也可以使用UIView- (void)updateConstraints选择器,如果您已经扩展了所述视图。

不能直接调用-updateConstraints。你必须把视图的constraints标记为dirty, iOS会在下一个更新周期调用那个方法。

在UIViewController的实现中,当你需要改变约束时添加这个:

self.scrollViewHieghtConstraint.constant = 500;
[self.view setNeedsLayout];

我假设UIScrollView是UIViewController的视图的子视图。如果不是,则在scrollview的父视图上调用-setNeedsLayout。

如果你需要知道子视图何时已经在正确的位置,你有一个UIViewController的回调,叫做-viewDidLayoutSubviews

最新更新