- 滚动视图滚动其帧大小必须小于其内容大小
- 您已经以编程方式添加了 scrollView FROM 代码,但看起来您正在谈论通过 Xcode 的 Storyboard 或在 nib 文件中添加它
- 如果您要添加它认为代码,则需要添加以下代码
[self.view addSubView:scrollView];
这是一个例子
@interface ImageScrollView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@end
-(void) baseInit {
// Scroll View - Full frame width
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.frame;
_scrollView.delegate = self;
// ImageView - Full frame width
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
int i = 0;
int count = [imagesArray count];
for (i = 0; i < count; i++) {
UIImage *image = [UIImage imageNamed:imagesArray[i]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
[imageView setImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = i+1;
[_scrollView addSubview:imageView];
}
[_scrollView setContentSize:CGSizeMake(count * width, height)];
[_scrollView setFrame:CGRectMake(0, 0, width, height)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
pageControl.numberOfPages = count;
pageControl.currentPage = 0;
CGRect pageFrame = CGRectMake(5, 5, 50, 50);
pageControl.frame = pageFrame;
pageControl.backgroundColor = [UIColor redColor];
[self addSubview:_scrollView];
[_scrollView addSubview:pageControl];
}
不要忘记在您的视图中调用 baseInit 函数确实加载了。此示例无需在 Storyboard 或 nib 文件中添加任何对象即可工作