UIScrollView以编程方式创建contectview子视图的动态高度和宽度

时间:2016-05-05 05:35:12

标签: ios objective-c uiscrollview autolayout

我正在制作scrollview的分页视图。当我给出子视图的固定宽度和高度时,它工作得很好。

让我告诉你代码。

制作scrollview和contenview

 [self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.contectView];

为scrollview和contenview添加约束

 NSDictionary *views = @{@"scrollView" : self.scrollView , @"contectView" : self.contectView };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:views]];

[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contectView]|" options:0 metrics:0 views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contectView]|" options:0 metrics:0 views:views]];

在竞赛视图中添加子视图

for (int i=0; i<10; i++) {

    UIView *productView =[self creatProductView];
    [self.contectView addSubview:productView];

 NSDictionary *views = @{@"productView" : productView , @"contectView" : self.contectView };
    if (i==0) {
        [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[productView(==375)]" options:0 metrics:0 views:views]];
    }
    else{
        views = @{@"productView" : productView , @"contectView" : self.contectView , @"lastView" : lastView , @"view" : self.view};
        [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastView][productView(==375)]" options:0 metrics:0 views:views]];

    }
    [self.contectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[productView(==667)]" options:0 metrics:0 views:views]];
    lastView = productView;
}

约束我需要改变:

    H:|[productView(==375)]
H:[lastView][productView(==375)]
V:|[productView(==667)]

如上所述,我给出了固定约束。我无法相对于 contectView ,因为它没有宽度和高度约束

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的。

self.scrollView  = [UIScrollView new];
self.contectView = [UIView new];
self.scrollView.translatesAutoresizingMaskIntoConstraints  = NO;
self.contectView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.contectView];

NSDictionary *views = @{@"scrollView" : self.scrollView , @"contectView" : self.contectView };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:views]];

//Adding equal width and height constraint with LOW priority
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contectView(==scrollView@250)]|" options:0 metrics:0 views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contectView(==scrollView@250)]|" options:0 metrics:0 views:views]];

NSString *horizontalConstraintsFormat = @"";
NSMutableDictionary *subViews = [NSMutableDictionary new];
subViews[@"scrollView"] = self.scrollView;
int count = 10;
for (int i = 0; i < count; i++) {
    //Create your content view, e.g.
    UIView *productView = [UIView new];
    productView.backgroundColor = [UIColor colorWithWhite:arc4random()%100 / 100.0f alpha:1];

    //Important line! Disable translation of autoresizing mask into constraints
    productView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contectView addSubview:productView];

    NSString *key = [NSString stringWithFormat:@"productView%d", i];
    subViews[key] = productView;
    if (i == 0) {
        //pin to the left side and set equal width to scrollview
        horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"|-0-[%@(==scrollView)]", key];
    } else if (i == count - 1) { //is the last one
        //pin to the prev item and right side and set equal width to scrollview
        horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"-0-[%@(==scrollView)]-0-|", key];
    } else {
        //pin to the prev item and set equal width to scrollview
        horizontalConstraintsFormat = [horizontalConstraintsFormat stringByAppendingFormat:@"-0-[%@(==scrollView)]", key];
    }

    //Set equal height to scrollview
    NSString *verticalConstraintsFormat = [NSString stringWithFormat:@"V:|[%@(==scrollView)]", key];
    [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraintsFormat options:0 metrics:nil views:subViews]];
}

//applying generated format
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontalConstraintsFormat options:0 metrics:nil views:subViews]];

RESULT =&gt;&gt;
enter image description here