我可以在同一个视图上使用 setFrame 和 autolayout 吗? [英] Can I use setFrame and autolayout on the same view?

查看:27
本文介绍了我可以在同一个视图上使用 setFrame 和 autolayout 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为所有按钮添加内边距,因此我将 UIButton 子类化,并且在其他更改中,我想使用 setFrame 方法添加固定内边距.一切正常,除了 setFrame.我检查了一下,我发现如果我取消选中该视图上的使用自动布局",那么我可以使用 setFrame,并且它可以工作.有没有解决的办法?我真的很想使用自动布局,因为它有助于使应用程序在 iphone 5 和更早的设备上看起来都不错.但我也想在我的子类中使用 setFrame,让我的生活更轻松.

I want to add padding to all of my buttons, so I subclassed UIButton, and among other changes, I wanted to add a fixed padding by using setFrame method. Everything was working, except for setFrame. I checked around, and I found out that if I uncheck "using AutoLayout" on that view, then I can use setFrame, and it works. Is there a way around this? I really want to use autolayout, because it helps in making the app look nice on both iphone 5 and earlier devices. But I also would like to use setFrame in my subclass, to make my life a litle easier.

总结一下,我的问题是:我可以使用自动布局并以编程方式调整 UIView 的框架吗?

Summing up, my question is: Can I use autolayout and also adjust the frame of a UIView programmatically?

推荐答案

是的,这是可以做到的.

Yes, this can be done.

如果您设置视图的 translatesAutoresizingMaskIntoConstraints = YES,则对 setFrame: 的调用会在运行时根据视图的当前 autoresizingMask 自动转换为布局约束代码>.这让您可以混合使用基于框架的布局和基于约束的布局.

If you set a view's translatesAutoresizingMaskIntoConstraints = YES, then calls to setFrame: are automatically translated at runtime into layout constraints based on the view's current autoresizingMask. This lets you mix frame-based layout with constraint-based layout.

例如,您可以使用自动布局来定义视图的所有子视图的布局,但仍然调用 setFrame: 来设置视图本身的大小和位置.从您的角度来看,您正在使用自动布局和直接框架操作的混合进行布局.但系统实际上是在使用约束来处理一切.

For instance you could use Auto Layout to define the layout of all of the subviews of a view, but still call setFrame: to set the size and position of the view itself. From your perspective, you're doing layout with a mix of Auto Layout and direct frame manipulation. But the system is actually using constraints to handle everything.

然而,使用 translatesAutoresizingMaskIntoConstraints 有一个很大的警告.

However, there is one big caveat about using translatesAutoresizingMaskIntoConstraints.

执行此操作时,您仍然需要确保这些自动约束可以满足其余约束.

因此,例如,假设已经存在确定视图大小和位置的约束,然后您设置 translatesAutoresizingMaskIntoConstraints = YES 并调用 设置框架:.对 setFrame: 的调用将在视图上生成新的约束,这可能会与现有的约束冲突.

So, for instance, suppose there are already constraints that determine the size and position of your view, and then you also set translatesAutoresizingMaskIntoConstraints = YES and called setFrame:. The call to setFrame: will generate new constraints on the view, which will probably conflict with the already existing constraints.

(事实上,这个错误经常发生.如果你看到一条日志消息抱怨约束冲突,而这些约束之一是NSAutoresizingMaskLayoutConstraint,那么你看到的是与一个自动约束.这是一个简单的错误,因为 translatesAutoresizingMaskIntoConstraints = YES 是默认值,所以如果你在代码中配置约束,如果你不想要这些,你需要记住将其关闭自动约束.)

(In fact, this error happens often. If you ever see a log message complaining about conflicting constraints, and one of those constraints is a NSAutoresizingMaskLayoutConstraint, then what you're seeing is a conflict with an automatic constraint. This is an easy mistake, because translatesAutoresizingMaskIntoConstraints = YES is the default value, so if you're configuring constraints in code you need to remember to turn it off if you don't want these automatic constraints.)

相反,再次假设已经存在确定视图大小和位置的约束,但是在调用 setFrame: 之前设置 translatesAutoresizingMaskIntoConstraints = NO.在这种情况下,您的 setFrame: 调用不会产生新的约束,因此在单独的约束之间不会有冲突.但是,在这种情况下,约束和您设置的框架值之间仍然存在冲突".下次调用 Auto Layout 时,它会看到视图上已经存在的约束,计算它们需要的框架值,并将框架设置为所需的值本身,破坏您手动设置的值.

In contrast, suppose again that there are already existing constraints that determine the size and position of your view, but then you set translatesAutoresizingMaskIntoConstraints = NO before you call setFrame:. In this case, your setFrame: calls would not produce new constraints, so there would be no conflict between separate constraints. However, in this case, there is still a "conflict" between the constraints and the frame value you set. At the next time Auto Layout is invoked, it would see the already existing constraints on the view, calculate the frame value which they require, and set the frame to the required value itself, clobbering the value you set manually.

有关更多详细信息,请查看 Apple Cocoa 自动布局指南.

For more details, check out the section "Adopting Auto Layout" in Apple's Cocoa Auto Layout Guide.

这篇关于我可以在同一个视图上使用 setFrame 和 autolayout 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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