繁体   English   中英

调整超级视图大小时自动调整子视图大小

[英]Autoresize subviews when resizing the superview

我无法正确调整子视图的大小:

假设我有一个自定义UIView类,其中包含UIImageView。

最初,我将UIViews设置如下:

// width and height are UIImage width height
// it is possible that the UIImage width height is larger than the container

self.imageView.frame = CGRectMake(0 ,0, width, height)
self.imageView.backgroundColor = UIColor.redColor()
self.imageView.contentMode = UIViewContentMode.ScaleAspectFit
self.imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight,.FlexibleTopMargin, .FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleBottomMargin]
addSubview(self.imageView)

self.frame = CGRectMake(0, 0, width, height)
self.backgroundColor = UIColor.greenColor()

我们将此自定义UIView称为MyView。

然后,当我要将这个“ myView”放置在ViewController中时。 我做了以下工作:

myView.frame = CGRectMake(xPos, yPos, 100, 100)
myView.autoResizeSubviews = true

我发现这样做之后,得到了以下结果。 MyView放置在具有正确大小的正确位置,但是内部UIImageView的大小更小。

为什么会这样呢? 以及如何根据MyView的框架正确调整MyView中的所有子视图的大小?

谢谢。

哦,我发现自动调整大小蒙版应设置如下:

self.imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight, .FlexibleRightMargin, .FlexibleBottomMargin]

这样可以将锚定点固定在顶部和左侧,并允许在其他边距上调整大小。

如果您不使用自动布局,请交换波纹管

请确保您在自行分配(UIView)之后设置框架

self.frame = CGRectMake(0, 0, width, height)
self.imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight,.FlexibleTopMargin, .FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleBottomMargin]
addSubview(self.imageView)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM