【发布时间】:2013-07-31 17:31:11
【问题描述】:
我有一个简单的布局,其中有一个在整个屏幕上开始的containerView。在里面,我有两个垂直的子视图——topView (green), botView (orange),它们需要相等的高度,并且应该有 containerView 高度的 50%。
启动后,我从屏幕外从底部向上滑动视图(蓝色),导致containerView 调整到较短的高度。
我期望发生的是topView 和botView 都会变短,在containerView 内保持它们的均匀高度,但实际发生的是内部视图没有调整大小,尽管有限制这样做。
因为这段代码有一些基于代码的约束,还有一些 IB 约束,here 是一个示例项目,可以看到它的实际运行情况。这是被忽略的约束:
NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:self.topView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.containerView
attribute:NSLayoutAttributeHeight
multiplier:0.5f
constant:0.f];
[self.containerView addConstraint:c1];
和调整大小的视图代码:
- (void)resizeViews {
__weak UAViewController *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
weakSelf.slideInView.frame = CGRectMake(CGRectGetMinX(weakSelf.view.bounds),
CGRectGetMaxY(weakSelf.view.bounds) - CGRectGetHeight(weakSelf.slideInView.frame),
CGRectGetWidth(weakSelf.view.bounds),
CGRectGetHeight(weakSelf.slideInView.frame));
weakSelf.containerView.frame = CGRectMake(CGRectGetMinX(weakSelf.view.bounds),
CGRectGetMinY(weakSelf.view.bounds),
CGRectGetWidth(weakSelf.view.bounds),
CGRectGetMaxY(weakSelf.view.bounds) - CGRectGetHeight(weakSelf.slideInView.frame));
}];
});
}
我在这里做错了什么?
【问题讨论】:
-
对了,你为什么要在
resizeViews中做dispatch_async?这没有必要(除非您从其他队列中调用它,如果您这样做我会感到惊讶)。但这没有实际意义,因为如果您正在执行自动布局,则根本不应该使用frame属性。修改您的约束,并让约束引擎做出适当的frame更改。 -
dispatch_async 正在为此调试应用程序的计时器线程调用。
标签: ios uiviewcontroller autolayout nslayoutconstraint