gpt4 book ai didi

ios - UIScrollView subview 不会调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:10 25 4
gpt4 key购买 nike

设计:
View 层次结构如下:

  • 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 的相对来源不正确(调整大小之前和之后)。

为了让 UIView 正确放置在 UIImageView 中,我应该做些什么?

最佳答案

这是“ ScrollView subview 不会调整大小”在 Google 上的热门话题。对于点击该链接的其他人:

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

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

关于ios - UIScrollView subview 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com