滚动视图问题iphone

时间:2014-08-20 06:57:17

标签: objective-c iphone ios7 uiscrollview

我需要使用页面控制器水平滚动视图。使用滚动视图我试图滚动视图。问题是视图不适合滚动视图,而水平滚动则上下移动。我已经禁用显示垂直指示器,垂直弹跳等。它仍然反复上下跳动。我使用的代码如下:

NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) 
{
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;       
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [colors objectAtIndex:i];
    [subview sizeToFit];
    [self.scrollView addSubview:subview];   
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;

1 个答案:

答案 0 :(得分:1)

试试这个:

float subviewWidth = 320;
float subviewHeight = 200;

NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
    float x = subviewWidth * i;  //width of your one view

    UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(x, 0, subviewWidth, subviewHeight)];//last two parameters is for your view's width and height
    subview.backgroundColor = [colors objectAtIndex:i];
    [self.scrollView addSubview:subview];  
}

self.scrollView.frame = CGRectMake (0,50,subviewWidth,subviewHeight);
self.scrollView.contentSize = CGSizeMake(subviewWidth * colors.count, subviewHeight); 
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;