2011-07-20 21 views
0

在我的viewController之一,我有一個滾動視圖,其中包含一個UIView和的UIImageView,UITextView的等在這裏滾動視圖就是我寫的代碼:如何通過UIScrollView工作?

overViewScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)]; 
     [overViewScroll setCanCancelContentTouches:NO]; 
     overViewScroll.pagingEnabled = YES; 
     overViewScroll.clipsToBounds = NO; 
     overViewScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
     [overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, screenFrame.size.height-25)]; 
     [overViewScroll setScrollEnabled:YES]; 
     [overViewScroll setAlwaysBounceVertical:YES]; 
     [overViewScroll setShowsVerticalScrollIndicator:YES]; 

UIView * completeView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)]; 
    UILabel* heading1 = [[UILabel alloc]initWithFrame:CGRectMake(5,0,310,30)]; 
    heading1.text = name; 
    heading1.backgroundColor = [UIColor clearColor]; 
    UIFont * headingFont = [UIFont fontWithName:@"HelVetica" size:15]; 
    heading1.font = headingFont; 
    [completeView addSubview:heading1]; 
    //NSSet * imageString = [projectHeroData valueForKey:@"imageURL"]; 
    NSString * urlString = @""; 
    NSURL * url = [NSURL URLWithString:urlString]; 
    NSData * imageData = [NSData dataWithContentsOfURL:url]; 
    UIImage * projImage = [[UIImage alloc]initWithData:imageData]; 
    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,screenFrame.size.height/50,screenFrame.size.width-((screenFrame.size.width/70)*2),screenFrame.size.height/3)]; 
    imgView.image = projImage; 
    imgView.backgroundColor = [UIColor blueColor]; 
    [completeView addSubview:imgView]; 

    UILabel * abstract = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width/4,screenFrame.size.height/30)]; 
    abstract.text = @"Abstract-"; 
    abstract.font = [UIFont boldSystemFontOfSize:fontSize]; 
    abstract.backgroundColor = [UIColor clearColor]; 
    [completeView addSubview:abstract]; 

    UILabel * lastUpdated = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41 + screenFrame.size.width/5,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width-screenFrame.size.width/4,screenFrame.size.height/30)]; 
    NSString * string = modDate ; 
    NSString * updated = [NSString stringWithFormat:@"Last Updated-%@",string]; 
    lastUpdated.text = updated; 
    lastUpdated.backgroundColor = [UIColor clearColor]; 
    lastUpdated.font = [UIFont boldSystemFontOfSize:fontSize]; 
    [completeView addSubview:lastUpdated]; 

    UITextView * textView2 = [[UITextView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,(screenFrame.size.height/31 + screenFrame.size.height/3)+screenFrame.size.height/30,screenFrame.size.width-screenFrame.size.width/70*2,screenFrame.size.height)]; 
    textView2.text = abst; 
    textView2.editable = NO; 
    textView2.font = [UIFont systemFontOfSize:fontSize]; 
    textView2.backgroundColor = [UIColor clearColor]; 
    [completeView addSubview:textView2]; 

    [overViewScroll addSubview:completeView]; 
    [self.view addSubview:overViewScroll]; 

一切addded到的UIView和添加的UIView到UIScrollView。 但問題是它的行爲怪異,圖像不是一直滾動,Textview中的文本自身正在滾動。 請幫忙!!

+0

更改您的滾動視圖的內容 – ajay

+0

我該怎麼做才能增加它或減少它? – Ashutosh

+0

當你想要滾動然後scrollview contentsize必須大於它的subview.means在你的情況下uiview框架大小小於uiscrollview – ajay

回答

0

更改scrollview.your UIview大小和uiscrollview大小的內容相同,如果UIScrollview內容大於其子視圖大小,則會發生滾動更改行[overViewScroll setContentSize:CGSizeMake(screenFrame.size.width,screenFrame。 size.height-25)];像這樣[overViewScroll setContentSize:CGSizeMake(screenFrame.size.width,(screenFrame.size.height-25)* 2)];它將允許你水平滾動默認情況下,textview具有scroll屬性的self。

+0

好吧,如果我不想讓textView有水平滾動視圖,不想讓它滾動本身而不是滾動整個視圖。我該怎麼做/ – Ashutosh

+0

確定了.....謝謝, – Ashutosh

0
overViewScroll =[[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)]; 
    [overViewScroll setCanCancelContentTouches:NO]; 
    overViewScroll.pagingEnabled = YES; 
    overViewScroll.clipsToBounds = NO; 
    overViewScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    [overViewScroll setContentSize:CGSizeMake(320,600)]; 
    [overViewScroll setScrollEnabled:YES]; 
    [overViewScroll setAlwaysBounceVertical:YES]; 
    [overViewScroll setShowsVerticalScrollIndicator:YES]; 

UIView * completeView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)]; 
UILabel* heading1 = [[UILabel alloc]initWithFrame:CGRectMake(5,0,310,30)]; 
heading1.text = name; 
heading1.backgroundColor = [UIColor clearColor]; 
UIFont * headingFont = [UIFont fontWithName:@"HelVetica" size:15]; 
heading1.font = headingFont; 
[completeView addSubview:heading1]; 
//NSSet * imageString = [projectHeroData valueForKey:@"imageURL"]; 
NSString * urlString = @""; 
NSURL * url = [NSURL URLWithString:urlString]; 
NSData * imageData = [NSData dataWithContentsOfURL:url]; 
UIImage * projImage = [[UIImage alloc]initWithData:imageData]; 
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,screenFrame.size.height/50,screenFrame.size.width-((screenFrame.size.width/70)*2),screenFrame.size.height/3)]; 
imgView.image = projImage; 
imgView.backgroundColor = [UIColor blueColor]; 
[completeView addSubview:imgView]; 

UILabel * abstract = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width/4,screenFrame.size.height/30)]; 
abstract.text = @"Abstract-"; 
abstract.font = [UIFont boldSystemFontOfSize:fontSize]; 
abstract.backgroundColor = [UIColor clearColor]; 
[completeView addSubview:abstract]; 

UILabel * lastUpdated = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41 + screenFrame.size.width/5,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width-screenFrame.size.width/4,screenFrame.size.height/30)]; 
NSString * string = modDate ; 
NSString * updated = [NSString stringWithFormat:@"Last Updated-%@",string]; 
lastUpdated.text = updated; 
lastUpdated.backgroundColor = [UIColor clearColor]; 
lastUpdated.font = [UIFont boldSystemFontOfSize:fontSize]; 
[completeView addSubview:lastUpdated]; 

UITextView * textView2 = [[UITextView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,(screenFrame.size.height/31 + screenFrame.size.height/3)+screenFrame.size.height/30,screenFrame.size.width-screenFrame.size.width/70*2,screenFrame.size.height)]; 
textView2.text = abst; 
textView2.editable = NO; 
textView2.font = [UIFont systemFontOfSize:fontSize]; 
textView2.backgroundColor = [UIColor clearColor]; 
[completeView addSubview:textView2]; 

[overViewScroll addSubview:completeView]; 
[self.view addSubview:overViewScroll]; 

試試這個,它會工作。另一種方法是使用界面構建器並選擇scoll視圖並將其他標籤圖像視圖拖放到滾動視圖上。