我可以使用SETFRAME和自动布局上同样的看法? [英] Can I use setFrame and autolayout on the same view?

查看:198
本文介绍了我可以使用SETFRAME和自动布局上同样的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要填充添加到所有我的按钮,所以我子类的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:. 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的帧programmaitcally

So summed up my question is: Can I use autolayout and also adjust the frame of a UIView programmaitcally?

推荐答案

是的,这是可以做到。

如果你设置视图的 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:。到 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 是默认值,所以如果你配置在code约束你需要记住关闭它,如果你不想这些自动限制。)

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

在此相反,再假设有已经存在决定视图的大小和位置的限制,但随后将 translatesAutoresizingMaskIntoConstraints = NO 你打电话之前 SETFRAME:。在这种情况下,你的 SETFRAME:通话不会产生新的约束,所以不会有什么冲突独立的限制的之间的。然而,在这种情况下,仍然有限制和设置的帧值之间的冲突的。在接下来的时间自动布局被调用时,它会看到已经存在的制约来看,计算出他们需要的帧值,并设置帧所需的值本身,重挫您手动设置的值。

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.

有关详细信息,请查看苹果节采用自动布局<一个href=\"https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html\">Cocoa自动布局指南。

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

这篇关于我可以使用SETFRAME和自动布局上同样的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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