【发布时间】:2015-02-14 00:15:47
【问题描述】:
我目前正在创建一组自定义视图以形成一个列表。但是,我遇到了 AutoLayout 的问题。
看起来视图的大小在函数调用之间发生了变化。以下是部分代码:
- (void)viewDidLoad
{
[self.scrollView layoutIfNeeded];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 50.0f)];
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView addSubview:self.contentView];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:0 views:@{@"contentView":self.contentView}]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:0 views:@{@"contentView":self.contentView}]];
第一个 layoutIfNeeded 是因为尺寸返回的宽度为 600(Interface Builder 中的默认值),这似乎会影响其余的尺寸。
但是,我的主要问题是在viewDidLoad 期间,scrollView 和 contentView 的大小返回正确。然后,如果我从viewDidLoad 中调用以下函数:
- (void)renderStreams
{
NSLog(@"Scroll View: %@", NSStringFromCGSize(self.scrollView.frame.size));
NSLog(@"Content View: %@", NSStringFromCGSize(self.contentView.frame.size));
它工作正常,尺寸正确返回。但是,我的代码调用了另一个类,该类异步加载我的数据并触发通知。在我当前的课程中,我像这样调用 renderStreams 函数:
dispatch_sync(dispatch_get_main_queue(), ^(void)
{
[self renderStreams];
});
现在,当我检查 scrollView 大小时,它已从 391 变为 375。更奇怪的是,contentView 已变为 0,0。
知道为什么会这样吗?
【问题讨论】:
标签: ios objective-c cocoa-touch autolayout