-1

我有ViewController一个ScrollView包含一堆内容的内容,IB但滚动视图不起作用。

- (void)viewDidLoad
{
    [super viewDidLoad];
/*
 *  configure a bunch of label, text ,images and views from IB
*/

    UIScrollView* scrollView = (UIScrollView*)self.view;
    scrollView.scrollEnabled = YES;
    scrollView.contentSize = CGSizeMake(320, 2000);
}

我可以看到我的内容直到底部,但无法滚动。如果它意味着什么,这ViewController在一个选项卡中TabBar

4

1 回答 1

2
  1. 滚动视图滚动其帧大小必须小于其内容大小
  2. 您已经以编程方式添加了 scrollView FROM 代码,但看起来您正在谈论通过 Xcode 的 Storyboard 或在 nib 文件中添加它
  3. 如果您要添加它认为代码,则需要添加以下代码

[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 文件中添加任何对象即可工作

于 2013-09-02T01:24:31.590 回答