【问题标题】:Add UIScrollView in UIView programmatically以编程方式在 UIView 中添加 UIScrollView
【发布时间】:2013-04-08 17:51:34
【问题描述】:

我有一个问题,如何在 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 重叠。那是我的问题。 解决了!

【问题讨论】:

  • 你看不到红色滚动视图吗?
  • 我看到第一个 UIView 屏幕 colorWithRed:0.5/i.
  • 猜猜,你的 awesomeView 比滚动视图本身的宽度和高度更大。这意味着滚动视图正在被添加,尝试增加滚动视图的框架和内容大小。
  • 我认为您的 xOrigin 计算错误,awesomeView 的,必须重叠
  • 我可以看到 UIViews。我不能只滚动 UIScrollView。

标签: objective-c uiview uiscrollview


【解决方案1】:

您可以使用以下代码在 yourView 上添加 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 委托

-(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
}

【讨论】:

    【解决方案2】:

    你的

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

    【讨论】:

      【解决方案3】:
      [container addSubview:scroll];
      

      在你的 for 循环之前添加这一行。

      【讨论】:

      • 它没有帮助。我可以看到滚动视图及其内容,但不能滑动。
      猜你喜欢
      • 2012-02-29
      • 2019-08-05
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多