3

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

  • UIScrollView(用于分页多个图像)
    • UIScrollView(用于启用缩放UIImageView
      • UIImageView
        • UIView(原点,大小相对于UIImagein UIImageView
        • 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];  
}  

以上是我尝试实现的代码。它将UIViews添加UIImageViewUIScrollView. 但是 的相对原点UIView不正确(调整大小之前和之后)。

UIView为了让s 正确放置在 s中,我应该做些什么不同的事情UIImageView吗?

4

1 回答 1

1

这是“滚动视图子视图不会调整大小”在谷歌上的热门话题。对于遵循该链接的其他任何人:

查看您的源代码并与我的源代码进行比较,我意识到在 Interface Builder 中,我的 NIB 文件的“自动大小子视图”复选框未选中。我有一个模糊的记忆,这可能是因为在旧版本的 Xcode 中(这是一个已经维护了一段时间的旧项目)...... UIScrollView 默认将其关闭。

所以:检查你是否在 Xcode/Interface Builder 中正确设置了它,和/或你在代码中设置它。

于 2013-05-10T09:58:57.967 回答