以编程方式在UIView中添加UIScrollView

时间:2013-04-16 10:47:22

标签: objective-c uiview uiscrollview

我有一个问题如何在UIView中添加UIScrollView,以便UIScrollView可以正常工作。

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
    CGFloat xOrigin = i * container.frame.size.width/2;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:awesomeView];
    [awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];

以上代码来自教程:http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/ 但它对我不起作用。

修改

如果您遇到问题,即您已正确设置了滚动视图但它无法正常工作,请确保您没有与另一个UIView覆盖您的滚动视图。那是我的问题。 的解决!

4 个答案:

答案 0 :(得分:6)

试试这个:

{
    scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.userInteractionEnabled=YES;
    [self.view addSubview:scrollview];
    scrollview.contentSize = CGSizeMake(width,height);
    [scrollview release];
}

答案 1 :(得分:1)

你的

scroll.contentSize = CGSizeMake(container.frame.size.width * numberOfViews, container.frame.size.height);

答案 2 :(得分:1)

[container addSubview:scroll];

在for循环之前添加此行。

答案 3 :(得分:1)

您可以使用以下代码在您的视图上添加UIScrollView: -

第1步:

Delegate "UIScrollViewDelegate" to your ViewController.h

for example:
  @interface yourViewController:UIViewController<UIScrollViewDelegate> 

第2步:

//create you UIScrollView
UIScrollView  *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)]; 
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)

第3步:

您想在ViewController.m中实现scrollView Delegates

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
   return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   NSLog(@"Did end decelerating");
   //do your code here
}   
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   NSLog(@"Did scroll");
   //do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"Did end dragging");
   //do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"Did begin decelerating");
   //do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
   NSLog(@"Did begin dragging");
   //do your code here
}