如何在滚动视图上为彼此相邻的增长视图添加约束?

时间:2015-03-06 05:37:41

标签: ios uiview uiscrollview nslayoutconstraint uiinterfaceorientation

即使在方向上,每个视图也应占据屏幕空间。 在水平滚动时,应出现下一个视图,该视图也适合屏幕尺寸 enter image description here

  - (void)viewDidLoad
    {
        [super viewDidLoad];
        _advProductNameArr=[[NSMutableArray alloc]init];
        [_advProductNameArr addObject:@"Product 1"];
        [_advProductNameArr addObject:@"Product 2"];
        [_advProductNameArr addObject:@"Product 3"];
        [_advProductNameArr addObject:@"Product 4"];

        _horizontalScroll.translatesAutoresizingMaskIntoConstraints = NO;
        //adding contstraints to horizontal scroll
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_horizontalScroll
                                                              attribute:NSLayoutAttributeTrailing
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.view
                                                              attribute:NSLayoutAttributeTrailing
                                                             multiplier:1.0
                                                               constant:0.0]];
       _drugCharArr=[[NSMutableArray alloc] init];

        CGFloat scrollWidth = 0.f;
        for (int i=0;i<[_advProductNameArr count];i++)
        {

        _productView=[[ProductTemplate alloc] initWithFrame:CGRectMake(scrollWidth, _horizontalScroll.bounds.origin.y,(_horizontalScroll.bounds.origin.x+_horizontalScroll.bounds.size.width), (_horizontalScroll.bounds.origin.y+_horizontalScroll.bounds.size.height))];

        // _productView=[[ProductTemplate alloc] init];

        _productView.productNameLbl.text =[NSString stringWithFormat:@"  %@  ",[_advProductNameArr objectAtIndex:i]];

        [_drugCharArr addObject:_productView.drugCharacterizationTxt];

        [_horizontalScroll addSubview:_productView];



        scrollWidth += _productView.bounds.size.width+10;

        }
        _horizontalScroll.contentSize = CGSizeMake(scrollWidth, _horizontalScroll.frame.size.height);

    }

此代码没有约束

0 个答案:

没有答案