Friday 15 January 2010

cocoa - Removing and re-adding a subview with autolayout -


While using Autolayout, I understand that removing a subview (while keeping a reference in it), the deleted subview still knows its autolayout obstructions

However, it is later found in Superview When adding back, the subview does not know its frame size anymore. Rather it is getting a zero frame.

I assumed that autolayouts will shape the size automatically to meet the obstacles. Is not that the case? I thought that auto layout does not mean frown with frame retrops. Do I still need to set the initial frame reset to add the preview with automatic layout?

When removing the subview, all obstacles related to that subview will be lost if you add a subview again later So, you have to add obstacles again on that subview.

Usually, I interfere with my custom subview. For example:

  - (zero) UpdateCustoms {if (! _myLayoutConstraints) {NSMutableArray * constraints = [NSMutableArray array]; // Make all your obstacles here [add constraints; some restraint]; // Save the barriers in an Iwer so that if updated consultants are called again, // we do not try to add them again. Exception will be _myLayoutConstraints = [NSArray arrayWithArray: Obstacles]; // Add Obstacles to Yourself, Custom SubView [Add Yourself: _myLayoutConstraints]; } [Super Update Consultants]; }   

Update Consultants will be automatically asked by Autolayout Runtime. The code above goes to its custom sub-class of the UIView .

You are right in working with Autolayout, you do not want to touch the frame size. Instead, just update the constraints in updateConstraints or, still better, set the barriers so that you do not want to.

See my answer on that topic:

You do not need to set the initial frame if you use initWithFrame , Then set it to CGRectZero .

For example, if you have a visual format: @ "| - [myView] - |" , it is necessary for horizontal dimensions to mark Autolayout with myView size of parents supervision | . This is very good.

No comments:

Post a Comment