【问题标题】:How to work through UIScrollView?如何通过 UIScrollView 工作?
【发布时间】:2011-07-20 03:32:39
【问题描述】:

在我的一个 viewController 中,我有一个 scrollView、一个 UIView,其中包含 UIImageView、UITextView 等。这是我写的代码:

    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];

一切都被添加到 UIView 和 UIView 被添加到 UIScrollView。 但问题是它的行为很奇怪,图像没有一直滚动,Textview 中的文本本身也在滚动。 请帮忙!!

【问题讨论】:

  • 更改滚动视图的内容大小
  • 我应该怎么做增加或减少它??
  • 当你想要滚动时,scrollview contentsize 必须大于它的 subview.means 在你的情况下 uiview frame size 小于 uiscrollview

标签: iphone objective-c uiscrollview


【解决方案1】:

更改滚动视图的内容大小。如果 UIScrollview 内容大小大于其子视图大小,则 UIview 大小和 uiscrollview 大小相同,则滚动将发生更改行 [overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, screenFrame.size .height-25)];像这样 [overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, (screenFrame.size.height-25)*2)];。它将允许您水平滚动并且默认情况下 textview 具有其自身的滚动属性。

【讨论】:

  • 好的,所以如果我不希望 textView 具有水平滚动视图并且不希望它本身滚动而只是滚动整个视图。我该怎么办/
【解决方案2】:
 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 视图并将其他标签图像视图拖放到滚动视图上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2011-04-11
    相关资源
    最近更新 更多