如何从重置视图框架prevent自动版式? [英] How to prevent AutoLayout from resetting view frames?
问题描述
我有一个自动版式视图,并与不同的屏幕尺寸很好地工作。还有的屏幕上四处移动根据用户行为的一个元素,但。每当我导航到不同的屏幕,然后回到这个观点,这个元素被复位到原来的位置。
I have a view with AutoLayout and it works nicely with different screen sizes. There's one element on the screen though that moves around depending on user actions. Whenever I navigate down to a different screen then back to this view, this element gets reset to its original position.
我明白为什么会这样,但我怎么能prevent呢?我也希望它后第一时间被设置正确,但最初没有。
I understand why this happens but how can I prevent it? I do want it to be setup correctly initially but not after the first time.
推荐答案
如果您正在使用自动布局,你不应该使用SETFRAME /因为的setBounds如您所见自动布局忽略它们并重写他们何时更新。
If you're using Autolayout you should never use setFrame/setBounds because as you've seen Autolayout ignores them and rewrites them when it updates.
的解决方案是改变的约束。约束有一个叫做常量参数
这是您可以在创建后对约束更改的唯一的事情。您还可以添加/当你需要移动NSViews删除多余的约束。
The solution is to change the constraints. A constraint has a parameter called constant
which is the only thing you can change on a constraint after it has been created. You can also add/remove extra constraints when you need to move NSViews around.
有来自WWDC 2012的影片,有如何使用自动版式移动的观点一个很好的例子。
There is a video from WWDC 2012 that had a very good example of how to move views with AutoLayout.
这篇关于如何从重置视图框架prevent自动版式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!