How to prevent AutoLayout from resetting view frames?

白昼怎懂夜的黑 提交于 2019-12-29 02:00:10

问题


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.

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.


回答1:


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.

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.

There is a video from WWDC 2012 that had a very good example of how to move views with AutoLayout.




回答2:


The only solution I found is, Implement viewDidLayoutSubviews

It will invoke the layoutSubviews method just after the view is loaded..

- (void)viewDidLayoutSubviews
{
    @try
    {
         // Adjusting controls.
    }
    @catch (NSException *exception)
    {
        NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
    }
}


来源:https://stackoverflow.com/questions/15423352/how-to-prevent-autolayout-from-resetting-view-frames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!