【问题标题】:How to correctly work with UIScrollView and UIPageControl?如何正确使用 UIScrollView 和 UIPageControl?
【发布时间】:2012-12-01 18:50:27
【问题描述】:

我有一个UIScrollView,我想将它与UIPageControl 一起使用,以使不同的页面水平滑动。我需要为每一页添加一个UITextView 和一些UIImageView。 在我的ViewController 中,我调用了一个网络服务,当数据到达时,我添加如下项目:

for(int i=0;i<[arrData count];i++) {
    NSString *body=@"Dummy text";

    CGRect frame;

    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size.width = self.scrollView.frame.size.width;
    frame.size.height = 40; //vedere landscape

    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:body];
    label.numberOfLines=0;
    [label sizeToFit];
    label.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.scrollView addSubview:label];

    [label setBackgroundColor:[UIColor whiteColor]];
    [self.scrollView addSubview:label];
    int y_pos=label.frame.size.height+10;
    int x_pos=0;
    for(int img=0; img<[[[arrData objectAtIndex:i]objectForKey:@"images"] count]; img++) {
        NSString *imageURL=[[[[arrData objectAtIndex:i] objectForKey:@"images"] objectAtIndex:img] objectForKey:@"fn"];

        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
        UIImage *image = [UIImage imageWithData:imageData];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [imageView setFrame:CGRectMake(self.scrollView.frame.size.width * i+img*100, y_pos, image.size.width, image.size.height)];
        [self.scrollView addSubview:imageView];

    }

}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [arrData count], self.scrollView.frame.size.height);

它在纵向上运行良好,但是当设备旋转时,布局显然被破坏了......这是使用ScrollViewPageControl 的最佳方式?我是否必须处理旋转并更改插入项目之间的距离,或者(我认为)是否有更好的方法来构建所有内容?

【问题讨论】:

  • 你在使用自动布局吗?如果是,请使用约束而不是设置框架。否则,如果您想完全控制布局(旋转时将框架设置为新值),则必须处理旋转。我在页面控件中使用带有滚动视图的自动布局,并且旋转工作正常(不需要额外的代码)。
  • 其实我没有使用自动布局。
  • 你的意思是布局被破坏了?你设置的框架会改变吗?还是它在做它应该做的事情(帧保持不变)但看起来很糟糕?
  • 帧保持不变;当我转到横向时,我看到第一帧和第二帧的一部分
  • 最好的方法是使用自动布局,但如果你不能,那么重新计算帧会给你最大的定制。 stackoverflow.com/questions/7443943/…

标签: ios uiscrollview uipagecontrol


【解决方案1】:

是的,每次方向变化时,您都必须重新设置滚动视图框架,因为在横向模式下,灵活宽度会从两侧拉伸滚动视图的宽度,因此当您进行水平滚动时,其内容会重叠并导致问题。您可以做的一件事是将滚动视图的自动调整大小掩码设置为flexibleRightMargin|flexibleLeftMargin。它将滚动视图的内容保持在中心,但滚动看起来不干净。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2015-09-02
    • 2011-07-02
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多