【问题标题】:UIImageView Define Size without .frameUIImageView 定义没有 .frame 的大小
【发布时间】:2012-09-14 01:17:53
【问题描述】:

我有一个启用分页的 UIScrollView。我想用图像填充它,并且我有一个 UIImageView 需要在不使用 .frame 的情况下进行调整。这是因为它不适用于启用自动布局。不使用自动布局不是一种选择。

代码如下:

//Prepare and load the view
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))];
scrollView1.pagingEnabled = YES;
[scrollView1 setShowsHorizontalScrollIndicator:NO];

CGRect Rect = scrollView1.bounds;
UIImageView *beadContainer;

for (int i = 0; i < imageViews.count; i++)
{
    beadContainer = [imageViews objectAtIndex:i];
    beadContainer.frame = Rect;
    [scrollView1 addSubview:beadContainer];
    Rect.origin.x += Rect.size.width;
}

尽管 ScrollView 具有所有正确的尺寸并按预期滚动,但没有任何图像按原样显示。如果我注释掉beadContainer.frame = Rect;,那么数组imageViews 中的所有图像都会出现在0, 0。它们都出现在彼此之上。当然,我需要它们来填充 ScrollView。

【问题讨论】:

    标签: ios6 iphone-5 xcode4.5


    【解决方案1】:

    为什么不使用自动布局来布局图像?

    //Prepare and load the view
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))];
    scrollView1.pagingEnabled = YES;
    [scrollView1 setShowsHorizontalScrollIndicator:NO];
    
    
    
    UIImageView *firstImage = imageViews[0];
    UIImageView *lastImageView = [imageViews lastObject];
    UIImageView *previousImageView = nil;
    NSMutableArray *constraints = [NSMutableArray new];
    for (UIImageView *imageView in imageViews){
        [scrollView1 addSubview:imageView];
        //set the size of the images
        [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView
                                                            attribute:NSLayoutAttributeWidth
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:NSLayoutAttributeNotAnAttribute
                                                           multiplier:1.0f
                                                             constant:kScrollObjWidth]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView
                                                            attribute:NSLayoutAttributeHeight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:NSLayoutAttributeNotAnAttribute
                                                           multiplier:1.0f
                                                             constant:kScrollObjHeight]];
        //remove autoresizing masks
        [imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
        //pin the top of the imageview to the top of the superview
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"V:|[imageView]"
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:NSDictionaryOfVariableBindings(imageView)]];
    
        if ([firstImage isEqual:imageView]){ //pin the first image view to the left of the scrollview
            [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"|[firstImage]"
                                                                                    options:0
                                                                                    metrics:nil
                                                                                      views:NSDictionaryOfVariableBindings(firstImage)]];
        }
    
        if (previousImageView){ //pin any imageViews to the previous imageViews
            [constraints addObjectFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"[previousImageView][imageView]"
                                                                                   options:0
                                                                                   metrics:nil
                                                                                     views:NSDictionaryOfVariableBindings(imageView, previousImageView)]];
        }
        previousImageView = imageView;
    }
    
    [scrollView1 addConstraints:constraints];
    

    请注意,我没有尝试过,可能是垃圾代码,我是在文本编辑器中编写的。

    【讨论】:

    • 哇。谢谢你写这个。我会修补它,看看它是否能完成工作。思想约束和自动布局应该让我的生活更轻松,但这并非易事。再次感谢,我会回来报告的!
    • 代码可能很长,但与通过数学设置框架相比,它们实际上更容易阅读和理解。特别是,但在你的情况下,你在每个视图之间设置了一些填充,所有的小事情加起来就是一个混乱的集团。加空格的代码和上面的代码差别不大,只是把[previousImageView][imageView]改成[previousImageView]-[imageView]
    • 经过这一步,它变得更有意义了。你是对的。更具可读性。不过,有一个问题。消除一些问题后,我无法再与图像进行交互。您对为什么会这样有任何想法吗?我会继续努力,如果我修复它会报告。
    • 据我所知,scrollView1 似乎实际上并没有收到滑动动作或进展。这可能是由于imageView in imageViews 造成的吗?
    • 应该不是因为 for 语句块,检查你的滚动视图的内容大小。
    【解决方案2】:

    如果您要完全使用自动布局,请不要设置滚动视图的contentSize。只需使用约束完全布置内容,确保描述每个内容子视图的大小,以及内容子视图与围绕它们的假想矩形边界的距离。运行时将自动使用该虚构的矩形来创建内容大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多