1.ScrollView以及在ScrollView上面添加的view,一定不能用layout自动布局,楼主一开始使用Masonry自动布局,scrollview只能显示第一个view,pagecontroll显示正常。话不多说,代码如下:
这是显示正常的:
- (void)initUI {
pageNumbers = 2;
bounds = self.frame;
view1 = [UIView new];
[view1 setFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
view1.backgroundColor = [UIColor greenColor];
view2 = [UIView new];
[view2 setFrame:CGRectMake(bounds.size.width, 0, bounds.size.width, bounds.size.height)];
view2.backgroundColor = [UIColor yellowColor];
scrollView = [UIScrollView new];
[scrollView setFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[self addSubview:scrollView];
[scrollView addSubview:view1];
[scrollView addSubview:view2];
[scrollView setContentSize:CGSizeMake(bounds.size.width * pageNumbers, bounds.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setBounces:NO];
[scrollView setShowsHorizontalScrollIndicator:NO];
scrollView.delegate = self;
pageControl = [UIPageControl new];
[self addSubview:pageControl];
[pageControl setFrame:CGRectMake(bounds.size.width/2 - [NSNumber getTransNumberCompareWidth:40], bounds.size.height - [NSNumber getTransNumberCompareWidth:80], [NSNumber getTransNumberCompareWidth:80], [NSNumber getTransNumberCompareWidth:80])];
[pageControl setNumberOfPages:pageNumbers];
pageControl.currentPage = 0;
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; //设置未激活的指示点颜色
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //用户点击UIPageControl的响应函数
}
- (void)layoutSubviews{
[super layoutSubviews];
}
这是不能滑动的:
- (void)initUI {
pageNumbers = 2;
bounds = self.frame;
view1 = [UIView new];
view1.backgroundColor = [UIColor greenColor];
view2 = [UIView new];
view2.backgroundColor = [UIColor yellowColor];
scrollView = [UIScrollView new];
[self addSubview:scrollView];
[scrollView addSubview:view1];
[scrollView addSubview:view2];
[scrollView setContentSize:CGSizeMake(bounds.size.width * pageNumbers, bounds.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setBounces:NO];
[scrollView setShowsHorizontalScrollIndicator:NO];
scrollView.delegate = self;
pageControl = [UIPageControl new];
[self addSubview:pageControl];
[pageControl setNumberOfPages:pageNumbers];
pageControl.currentPage = 0;
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; //设置未激活的指示点颜色
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //用户点击UIPageControl的响应函数
}
- (void)layoutSubviews{
[super layoutSubviews];
//scrollView布局
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left);
}];
//view1布局
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left);
}];
//view2布局
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_right);
}];
//pagecontrol布局
[pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake([NSNumber getTransNumberCompareWidth:80], [NSNumber getTransNumberCompareWidth:80]));
make.bottom.mas_equalTo(self.mas_bottom);
make.centerX.mas_equalTo(self.mas_centerX);
}];
}
- (void)layoutSubviews{
[super layoutSubviews];
//scrollView布局
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left);
}];
//view1布局
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left);
}];
//view2布局
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(bounds.size.width, bounds.size.height));
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_right);
}];
//pagecontrol布局
[pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake([NSNumber getTransNumberCompareWidth:80], [NSNumber getTransNumberCompareWidth:80]));
make.bottom.mas_equalTo(self.mas_bottom);
make.centerX.mas_equalTo(self.mas_centerX);
}];
}