UIScrollView 子视图不会调整大小
Posted
技术标签:
【中文标题】UIScrollView 子视图不会调整大小【英文标题】:UIScrollView SubViews won't resize 【发布时间】:2013-01-04 23:02:12 【问题描述】:设计: 视图层次结构如下:
UIScrollView
(用于分页多张图片)
UIScrollView
(用于启用 UIImageView
的缩放)
UIImageView
UIView
(原点,大小相对于UIImage
in 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
s 添加到UIScrollView
内的UIImageView
。但是UIView
的相对来源不正确(调整大小之前和之后)。
为了让UIView
s 正确放置在UIImageView
中,我应该做些什么不同的事情吗?
【问题讨论】:
不清楚你在问什么。您希望图像视图的子视图显示什么?有没有你做过的事情不起作用,或者你只是在问这是否会起作用? 西蒙,我已经编辑了这个问题。我还添加了我尝试实现的代码 sn-p。希望这有助于使其更加清晰。谢谢。 【参考方案1】:这是“滚动视图子视图不会调整大小”在 Google 上的热门话题。对于点击该链接的其他人:
查看您的源代码,并与我的源代码进行比较,我意识到在 Interface Builder 中,我的 NIB 文件的“自动大小子视图”复选框未选中。我有一个模糊的记忆,这可能是因为在旧版本的 Xcode 中(这是一个维护了一段时间的旧项目)...... UIScrollView 默认关闭。
所以:检查您在 Xcode/Interface Builder 中的设置是否正确,和/或您是否在代码中进行了设置。
【讨论】:
以上是关于UIScrollView 子视图不会调整大小的主要内容,如果未能解决你的问题,请参考以下文章
Xcode UIScrollView 布局 - 设置水平内容大小并水平调整子视图大小
使用 UIScrollView 和 AutoLayout 以编程方式创建控制器无法正确调整视图大小