设计
查看层次结构如下:
UIScrollView
(用于分页多张图片)
UIScrollView
(用于启用UIImageView
上的缩放功能)
UIImageView
UIView
(相对于UIImage
中的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];
}
以上是我尝试实施的代码。它会将UIView
添加到UIImageView
内的UIScrollView
。但UIView
的相对来源不正确(在调整大小之前和之后)。
我是否应该采取不同的方式将UIView
正确放置在UIImageView
内?
答案 0 :(得分:1)
这是谷歌“滚动视图子视图不会调整大小”的最高点。对于关注该链接的其他人:
查看你的来源,并与我的相比,我意识到在Interface Builder中,我的NIB文件有“自动大小子视图”未选中的复选框。我有一个模糊的记忆,这可能是因为在旧版本的Xcode中(这是一个已经维护了一段时间的旧项目)...... UIScrollView默认关闭它。
所以:检查你是否在Xcode / Interface Builder中正确设置了它,和/或你是在代码中设置它。