自动调整子视图调整的上海华

问题描述:

当我不能够处理子视图正确调整:自动调整子视图调整的上海华

假设我有一个自定义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 place at the correct place with correct size, but the internal UIImageView got even a smaller size.

为什么会发生这种情况?以及如何使MyView中的所有子视图根据MyView的框架正确调整大小?

谢谢。

,如果你不使用自动布局然后交换波纹线

请确保您在设置自后架(UIView的)分配

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

哦,我发现,自动尺寸口罩应是设置如下:

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

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