ios - 当我使用自动布局时,作为 scrollView 的 subview 的 View 颜色没有出现?

标签 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)]; 然后它的颜色和给我的一样。在这种情况下为灰色

最佳答案

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

关于ios - 当我使用自动布局时,作为 scrollView 的 subview 的 View 颜色没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32493489/

相关文章:

ios - 使用 Core Data 保存图像

ios - 使用 DebugLog() 而不是 NSLog()

ios - UIPickerView 获取另一个 UIPickerView 的值

ios - 我不知道使用 UIView.animate 或创建 UIImageView 方法有什么问题。我使用快速语言

iphone - 如何以编程方式创建自定义 View 类?

ios - 在 Xcode 9 的上下文中,“对象”对于类型查找是不明确的

ios - 请求失败 : unacceptable content-type: text/plain using AFNetworking 2. 0

ios - Firebase 不触发 Google Tagmanager

ios - 图像过滤

ios - 如何在iOS7中绘制透明的UIToolbar或UINavigationBar