UIScrollView的子视图不会自动调整大小

3

设计:
视图层次结构如下:

  • UIScrollView(用于分页多个图像)
    • UIScrollView(用于在UIImageView上启用缩放)
      • UIImageView
        • UIView(原点、大小相对于UIImageView中的UIImage
        • UIView
        • UIView...
    • UIScrollview
      • UIImageView
        • UIView
        • UIView...
    • UIScrollView...等等

实现:

- (id)init  
{  
self = [super init];  
if (self) {  
    // Custom initialization  
    self.minimumZoomScale = 1.0f;  
    self.maximumZoomScale = 4.0f;  
    self.zoomScale = 1.0f;  
    self.bouncesZoom = true;  
    self.bounces = true;  
    self.tag = 10;  
    self.alwaysBounceHorizontal = false;  
    self.autoresizesSubviews = YES;  

    self.zoomView = [[UIImageView alloc] init];
    self.zoomView.userInteractionEnabled = YES;
    self.zoomView.autoresizesSubviews = YES;
}

return self;
}  
- (void)displayImage:(UIImage *)image  
{    
// Set the image in the view  
[_zoomView setImage:image];  

// Set the Frame size to the size of image size  
// Ex. Resulting ScrollView frame = {0,0},{1250,1500}  
// Ex. Resulting ImageView frame = {0,0}, {1250,1500}  
CGRect scrollFrame = self.frame;  
scrollFrame.size.width = image.size.width;  
scrollFrame.size.height = image.size.height;  
self.frame = scrollFrame;  

CGRect iViewFrame = _zoomView.frame;  
iViewFrame.size.width = image.size.width;  
iViewFrame.size.height = image.size.height;  
_zoomView.frame = iViewFrame;  

// Add subviews before resetting the contentsize  
for (customClass* field in list.fields) {  

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake([field.x floatValue], [field.y floatValue], [field.width floatValue], [field.height floatValue])];  
    view.backgroundColor = [UIColor redColor];  
    view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  

    [_zoomView addSubview:view];  
}  

// Set the Frame size to the size of scrollview frame size  
// Ex. Resulting ScrollView Frame = {0,0},{320,460}  
// Ex. Resulting ImageView Frame = {0,0},{320,460}  
CGRect scrollViewFrame = self.frame;  
scrollViewFrame.size.width = 320.0f;  
scrollViewFrame.size.height = 460.0f;  
self.frame = scrollViewFrame;  

CGRect imageViewFrame = _zoomView.frame;  
imageViewFrame.size.width = self.bounds.size.width;  
imageViewFrame.size.height = self.bounds.size.height;  
_zoomView.frame = imageViewFrame;  


self.contentSize = _zoomView.bounds.size;  

[self addSubview:_zoomView];  
}  

以下是我尝试实现的代码。它将UIView添加到UIScrollView内部的UIImageView中。但是UIView的相对原点不正确(在调整大小之前和之后都是如此)。
是否有任何我应该做出不同的操作,以便正确放置UIViewUIImageView内部?

不清楚你的问题是什么。你想让图像视图的子视图显示什么?你是否已经尝试过某些方法但没有成功,还是只是在询问这种方法是否可行? - Simon Goldeen
Simon,我已经编辑了问题。我还添加了我尝试实现的代码片段。希望这可以使它更加清晰明了。谢谢。 - Soki
1个回答

1
这是在Google上搜索“scrollview subviews wont resize”后排名第一的结果。对于其他跟随该链接的人来说:
查看您的源代码并将其与我的进行比较,我意识到在Interface Builder中,我的NIB文件的“auto size subviews”复选框未被选中。我模糊地记得,这可能是因为在旧版本的Xcode中(这是一个已经维护了一段时间的旧项目)... UIScrollView默认关闭它。
所以:请检查您在Xcode / Interface Builder中是否正确设置了它,并/或者您是否在代码中设置了它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接