当我使用自动布局时,作为scrollView子视图的视图颜色没有出现?

时间:2015-09-10 05:01:19

标签: ios

我正在使用约束添加scrollView并且它正常工作。但它显示出奇怪的行为。

当我将scrollView的框架添加为[SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)];时,其颜色始终为clearColor,无论我对它有什么看法

代码:

UIScrollView *scrllView=[UIScrollView new];
[scrllView setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.view addSubview:scrllView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];


[scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];


UIView *SBV=[UIView new];
[SBV setTranslatesAutoresizingMaskIntoConstraints:YES];

[SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)];
[SBV setBackgroundColor:[UIColor grayColor]];

[scrllView addSubview:SBV];

当我将view的框架添加为[SBV setFrame:CGRectMake(0, 0,self.view.frame.size.width,1000)];时,其颜色与给我的颜色相同。在这种情况下为灰色

2 个答案:

答案 0 :(得分:1)

不要使用CGRectMake函数,而是保持大小不变​​。它不会表现出任何奇怪的行为。

答案 1 :(得分:0)

使用Autolayout时设置内容大小确实无效。

请参阅此回答 - UIScrollView contentSize not working

此外 - UIScrollView doesn't use autolayout constraints