Iphone 无法在滚动视图中显示控件外接程序';使用自动布局创建内容视图
我是新的自动布局部分。我正在用scroll编程创建一个视图,但我无法在scroll视图的内容视图中添加控件 这是我的密码:Iphone 无法在滚动视图中显示控件外接程序';使用自动布局创建内容视图,iphone,ios,ipad,nslayoutconstraint,autolayout,Iphone,Ios,Ipad,Nslayoutconstraint,Autolayout,我是新的自动布局部分。我正在用scroll编程创建一个视图,但我无法在scroll视图的内容视图中添加控件 这是我的密码: -(void)setConstraints { UIScrollView *scrollView = [UIScrollView new]; [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; scrollView.backgroundColor = [UIColor gree
-(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];
}
显示的视图只是白色屏幕。我做错了什么?例如,必须为尺寸添加约束
[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]];
抱歉@jaba,但是它显示了视图。你在哪里调用该方法?它应该显示300-300大小的绿色视图。是的,它现在显示的是scrollview@jaba。但我仍然需要做更多的事情。之后我会告诉你最新情况。如果我找不到路。