gpt4 book ai didi

iphone - 无法使用自动布局显示控件添加到 ScrollView 的内容 View 中

转载 作者:行者123 更新时间:2023-11-29 03:33:43 24 4
gpt4 key购买 nike

我是自动布局部分的新手。我正在以编程方式创建一个带有滚动的 View ,但无法在 ScrollView 的内容 View 中添加控件

这是我的代码:

-(void)setConstraints
{

UIScrollView *scrollView = [UIScrollView new];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
scrollView.backgroundColor = [UIColor greenColor];

UIView *contentView = [UIView new];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
contentView.backgroundColor = [UIColor redColor];

UILabel *lableTitle = [UILabel new];
lableTitle.text = @"Registration";
[lableTitle setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *layoutLableTop = [NSLayoutConstraint constraintWithItem:lableTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
[self.view addConstraint:layoutLableTop];

NSLayoutConstraint *layoutLableCenterX = [NSLayoutConstraint constraintWithItem:lableTitle attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
[self.view addConstraint:layoutLableCenterX];

[contentView addSubview:lableTitle];
[scrollView addSubview:contentView];

[self.view addSubview:scrollView];


}

显示的 View 只是白屏。我做错了什么?

最佳答案

例如,您必须添加尺寸限制

[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:200]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:200]];

关于iphone - 无法使用自动布局显示控件添加到 ScrollView 的内容 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19449817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com