uiscrollview怎么添加约束
发布网友
发布时间:2022-10-12 01:28
我来回答
共1个回答
热心网友
时间:2023-10-16 20:04
用xcode创建一个新工程,然后打开ViewController.m文件
我们在ViewController的viewDidLoad方法里创建一个UIScrollView并且加上约束
代码如下:
//创建scrollView
UIScrollView * scrollView=[[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints=NO;
scrollView.backgroundColor=[UIColor lightGrayColor];
[self.view addSubview:scrollView];
//给它加上约束 使它距屏幕的上左下右都是20像素
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[scrollView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[scrollView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
效果如图
我要再创建两个view 这两个view的大小和scrollview的大小一样大,使scrollview能横向滚动
代码如下:
//添加约束 (非常重要)
//1.水平方向排开 宽度相等 上下边缘对齐(等高)
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[redView]-0-[blueView(==redView)]-0-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:NSDictionaryOfVariableBindings(redView,blueView)]];
//2.垂直方向一直撑到父视图的
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[redView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(redView)]];
//到了这里 还没完 如果是普通的view 那就搞定了 但是scrollview不一样还得添加下面两个约束
//3.redview的宽高 和scrollview的宽高相等的约束
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
整个viewdidload全部代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
//创建scrollView
UIScrollView * scrollView=[[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints=NO;
scrollView.backgroundColor=[UIColor lightGrayColor];
[self.view addSubview:scrollView];
//给它加上约束 使它距屏幕的上左下右都是20像素
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[scrollView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[scrollView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
//创建redView
UIView * redView=[[UIView alloc] init];
redView.backgroundColor=[UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints=NO;
[scrollView addSubview:redView];
//创建blueView
UIView * blueView=[[UIView alloc] init];
blueView.backgroundColor=[UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints=NO;
[scrollView addSubview:blueView];
//添加约束 (非常重要)
//1.水平方向排开 宽度相等 上下边缘对齐(等高)
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[redView]-0-[blueView(==redView)]-0-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:NSDictionaryOfVariableBindings(redView,blueView)]];
//2.垂直方向一直撑到父视图的
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[redView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(redView)]];
//3.redview的宽高 和scrollview的宽高相等的约束
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
}