即使在方向上,每个视图也应占据屏幕空间。 在水平滚动时,应出现下一个视图,该视图也适合屏幕尺寸
- (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);
}
此代码没有约束