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

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

问题描述

我想为所有的按钮添加填充,所以我继承了UIButton,在其他变化中,我想使用setFrame方法添加一个固定的填充。一切都工作,除了setFrame。我检查了一下,我发现如果我取消选中使用AutoLayout该视图,那么我可以使用setFrame,它的工作原理。有办法解决这个问题吗?我真的想使用autolayout,因为它有助于使应用程序在iPhone 5和更早的设备看起来不错。但我也想在我的子类中使用setFrame,使我的生活更轻松。



总结,我的问题是:我可以使用autolayout,

$ p








如果您设置视图的 translatesAutoresizingMaskIntoConstraints = YES ,则对 setFrame:的调用会在运行时自动转换为布局基于视图当前 autoresizingMask 的约束。这可让您将基于框架的布局与基于约束的布局混合。



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



但是,使用 translatesAutoresizingMaskIntoConstraints 有一个很大的警告。



执行此操作时,您仍需要确保这些自动限制条件可以满足其余的限制条件。



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



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



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



有关详情,请参阅Apple的 Cocoa Auto Layout Guide


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.

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

解决方案

Yes, this can be done.

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.

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.

However, there is one big caveat about using translatesAutoresizingMaskIntoConstraints.

When you do this, you still need to make sure that these automatic constraints can be satisfied with the rest of your constraints.

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.

(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.)

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.

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

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

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