使用addSubview添加视图时,会将视图大小调整为其父级 [英] Making view resize to its parent when added with addSubview

查看:227
本文介绍了使用addSubview添加视图时,会将视图大小调整为其父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用addSubview时遇到问题。

I'm having a problem when using addSubview.

示例代码:

ParentView *myParentView = [[ParentView alloc] initWithNibName:@"ParentView " bundle:nil];
ChildeView *myChildeView = [[ChildeView alloc] initWithNibName:@"ChildeView" bundle:nil];

//...  parent frame resized with setFrame lets say to x:0, y:0, W:320, H:411

[[myParentView view] addSubview: [myChildeView view]];

我的孩子在添加时比父母大,并且不会将其框架调整为父边界。我无法在父级上使用剪辑子视图,并且如果不再次调整父级框架,则自动调整子视图似乎不起作用。是否存在一个属性,使子视图自动调整为其父级边界,而不对每个子节点使用setFrame?

My child when added is bigger then the parent, and does not resize its frame to parent bounds. I can't use "clip subviews" on the parent, and "Autoresize Subviews" seems not to work if the parent frame is not resized again. Is there a property that makes a subview resize automatically to its parent's bounds without using setFrame on every child?

推荐答案

如果你不是使用自动布局,您是否尝试过设置子视图的自动调整大小?试试这个:

If you aren’t using Auto Layout, have you tried setting the child view’s autoresize mask? Try this:

myChildeView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                 UIViewAutoresizingFlexibleHeight);

此外,您可能需要致电

myParentView.autoresizesSubviews = YES;

以使父视图在其帧更改时自动调整其子视图的大小。

to get the parent view to resize its subviews automatically when its frame changes.

如果您仍然在父视图框架之外看到子视图绘图,则父视图很可能不会剪切其内容。要解决此问题,请致电

If you’re still seeing the child view drawing outside of the parent view’s frame, there’s a good chance that the parent view is not clipping its contents. To fix that, call

myParentView.clipsToBounds = YES;

这篇关于使用addSubview添加视图时,会将视图大小调整为其父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆