Scrollview内容显示但不滚动

时间:2013-09-02 01:08:42

标签: ios objective-c cocoa-touch uiscrollview

我有一个ViewControllerScrollView包含来自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

1 个答案:

答案 0 :(得分:2)

  1. 要滚动滚动视图,其框架大小必须小于其内容大小
  2. 您已经以编程方式添加了scrollView FROM代码,但看起来您正在谈论通过Xcode的Storyboard或nib文件添加它
  3. 如果您要添加思想代码,则需要添加以下代码
  4. [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文件中添加任何对象