【问题标题】:UIScrollView pages in the negative direction负方向的 UIScrollView 页面
【发布时间】:2015-03-25 14:23:07
【问题描述】:

所以我有一个 UIScrollView 设置如下,由于某种原因,滚动视图显示如下:

                  Phone Screen

                ---------------
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|----Content----|
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|---------------|
                ---------------

我的目标是让用户向右滚动,如下所示:

  Phone Screen

 ---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|----Content----|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
 ---------------

以下是滚动视图的设置方式(这有点简化,但准确):

CGFrame frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGSize contentSize = CGSizeMake(self.bounds.size.width*2, self.bounds.size.height);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = contentSize;
scrollView.propertyN = bool; //E.g. pagingEnabled, bounces, clipsToBounds, etc.

UIImageView *imageView = [[UIImageView alloc] initWithImage:someImage];
imageView.frame = frame;

[scrollView addSubview:imageView];
[self.view addSubview:scrollView];

还有其他人遇到过这个问题吗?我确定我在设置滚动视图时犯了一些非常基本的错误,但似乎无法发现它。

【问题讨论】:

  • 只能是 (a) contentOffset 被设置为 x=bounds.size.width, y=0, 或 (b) 滚动视图的某些后代 --subview 或子视图的子视图,等等——有一个负x位置的框架,或者(c)我想不出的其他原因。
  • @danh - 我已经检查并且 contentOffset 设置为 (0,0),唯一的子视图是我添加的这个图像视图,它的原点对于 x 和 y 都是肯定的.
  • 那我难住了。将 imageView.frame 设置为其父框架有点奇怪(尽管我认为这不是问题,因为它的原点是 0,0)。如果图像是唯一的子视图,并且 contentSize 是滚动视图宽度的 2 倍,你不希望你的图像也那么宽吗?
  • @danh - subview的宽度不应该等于scroll view的宽度,contentSize是subview宽度的倍数吗?
  • contentSize 通常足够大,可以包含 scrollView 的内容。将其设置为 2WxH 通常意味着第一代后代(在您的情况下为图像)占据该大小。

标签: ios objective-c uiscrollview scroll pagination


【解决方案1】:

self.view.frameorigin 是否会在屏幕外(例如 -self.bounds.width)?

我会尝试从简单的代码开始,然后构建到您想要的内容。下面是一些与上面的代码类似的简单代码,您可以将其放入一个空白项目中,以说服自己您发布的代码可能不是罪魁祸首:

  CGFloat width = self.view.bounds.size.width;
  CGFloat height = self.view.bounds.size.height;
  CGRect frame = CGRectMake(0.0f,0.0f,width,height);
  CGSize contentSize = CGSizeMake(width*2, height);

  UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
  scrollView.backgroundColor = [UIColor yellowColor];
  scrollView.contentSize = contentSize;
  scrollView.pagingEnabled = YES;

  UIView *view1 = [[UIView alloc] initWithFrame:frame];
  view1.backgroundColor = [UIColor blueColor];
  UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(width,0.0f,width,height)];
  view2.backgroundColor = [UIColor greenColor];

  [scrollView addSubview:view1];
  [scrollView addSubview:view2];
  [self.view addSubview:scrollView];

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多