2013-04-16 117 views
8

我有一個問題如何在UIView中添加UIScrollView,以便UIScrollView可以正常工作。以編程方式在UIView中添加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覆蓋你的滾動視圖。那是我的問題。 解決!

+0

滾動視圖代表u能不能看到紅色滾動視圖? –

+1

我看到第一個UIView屏幕colorWithRed:0.5/i。 – PiotrCh

+0

猜測,你的awesomeView比滾動視圖本身更大的寬度和高度。這意味着滾動視圖正在增加,嘗試增加滾動視圖的框架和內容大小。 –

回答

1

scroll.contentSize = CGSizeMake(container.frame.size.width * numberOfViews, container.frame.size.height); 
1
[container addSubview:scroll]; 

之前,你的for循環添加此行。

+0

它沒有幫助。我可以看到滾動視圖和它的內容,但我無法滑動。 – PiotrCh

+1

更改內容 – vshall

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

您可以使用下面的代碼添加的UIScrollView上yourView: -

第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

-(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 
} 
相關問題