嵌入在编程 UIView 中的自动布局 XIB 不调整到父级 [英] Auto-layout XIB embedded in programmatic UIView not sizing to parent

查看:12
本文介绍了嵌入在编程 UIView 中的自动布局 XIB 不调整到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xib 文件,它表示一个带有自动布局的简单视图,我用 [[NSBundle mainBundle] loadNibNamed:@"name"][0] 实例化它并添加到以编程方式创建的UIView 通过 addSubview:.

I have a xib file representing a simple view with auto-layout that I am instantiating with [[NSBundle mainBundle] loadNibNamed:@"name"][0] and adding to a programmatically-created UIView via addSubview:.

问题是视图没有被拉伸以满足它嵌入的视图的大小;相反,它似乎默认为 xib 文件中指定的大小.(我也找不到任何方法在 xib 文件中对容器视图本身设置约束,只有它的子视图.)

The problem is that the view is not stretching to meet the size of the view it is embedded in; rather it appears to be defaulting to the size that was specified in the xib file. (I also can't find any way to set constraints on the container view itself in the xib file, only its subviews.)

还有点奇怪的是,当我旋转 iPad 时,视图的宽度会按比例进一步缩小.所以它显然是以某种方式连接起来的,但初始值错误.

Also somewhat strangely, when I rotate the iPad, the view's width further shrinks proportionately. So it's clearly wired up somehow, but with the wrong initial values.

为了让这个嵌入式视图正确地适应视图层次结构,我缺少什么连接?

What connection am I missing to get this embedded view to fit the view hierarchy properly?

屏幕截图(为调试添加了洋红色):

Screenshots (magenta colouring added for debugging):

旋转成纵向后:

推荐答案

它没有调整自己的大小来填充它的新 superview 的原因是你没有安排它这样做.简而言之,您需要在代码中添加约束,以便在将一个视图添加到另一个视图之后将两个视图关联起来.

The reason it doesn't resize itself to fill its new superview is that you haven't arranged for it to do that. In short, you need to add constraints, in code, to relate the two views after adding one to the other.

最有可能的是,IB 将视图设置为将其自动调整掩码转换为约束.当它设置为使用自动布局时,它的自动调整大小掩码很难从 NIB 中确定.

Most likely, IB set the view to translate its autoresizing mask to constraints. What its autoresizing mask is can be hard to determine from the NIB when it's set to use auto layout.

无论如何,当您将其添加到超级视图时,自动生成的约束会保持其当前帧.当 superview 调整大小时,他们会根据 autoresizing mask 调整它的大小(可能允许宽度和高度变化,但不允许到 superview 边缘的距离).

Anyway, when you add it to the superview, the automatically generated constraints maintain its current frame. When the superview resizes, they resize it according to the autoresizing mask (probably allowing width and height to vary but not the distance to the superview edges).

当您使用自动布局时,您应该执行以下操作:

When you're using auto layout, you should do the following:

  • 要么在NIB中关闭translatesAutoresizingMaskIntoConstraints在将其添加到超级视图的代码中关闭它.您采用哪种方法取决于您是否预计该视图将被添加为框架视图类的子视图,该框架视图类管理其子视图的布局.如果可以,那么您应该将它留在 NIB 中,并让该类决定是否将其关闭.

  • Either turn off translatesAutoresizingMaskIntoConstraints in the NIB or turn it off in the code which adds it to the superview. Which of those approaches you take depends on whether you anticipate that the view will ever be added as a subview of a framework view class that manages the layout of its subviews. If you might, then you should leave it on in the NIB and let that class decide whether to turn it off.

将视图添加到其父视图后,向父视图添加约束,以控制视图的布局位置.

After adding the view to its superview, add constraints to the superview which controls where the view should be laid out.

这篇关于嵌入在编程 UIView 中的自动布局 XIB 不调整到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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