Why use layoutsubviews




















To put it bluntly is to draw an abstract drawing of some visible properties of the control. When is the timing of the call? The reverse meaning means: if you want to set the position of subviews externally, don't rewrite. Refresh the layout of sub-objects- setNeedsLayout method: mark as need to re-layout, asynchronously call layoutIfNeeded to refresh the layout, not immediately refresh, but layoutSubviews will be called -layoutIfNeeded method: If there is a mark that needs to be refreshed, immediately call layoutSubviews for layout if not Tag, layoutSubviews will not be called.

If you want to refresh immediately, you must first call [view setNeedsLayout], set the mark to need layout, and then immediately call [view layoutIfNeeded] to achieve the layout. Before the view is displayed for the first time, the mark is always "need to refresh", you can directly call [view layoutIfNeeded]. In iOS5. UIView subclasses can override this method if they need more precise layout of their subviews.

We should override this method only when autoresizing and constraint-based behaviors of subviews cannot provide the layout results we want. You can directly set the frame of subviews in this method. We should not call this method directly, but should use the following two methods.

This method will set the current layout of the view to invalid, and trigger the layout update in the next upadte cycle. Use this method to force immediate layout. Starting from the current view, this method will traverse the entire view hierarchy including superviews to request layout.

Therefore, calling this method will force the layout of the entire view hierarchy. AutoLayer's method based on constraints:. When a property of a custom view changes and may affect constraints, you need to call this method to mark the constraints that need to be updated at some point in the future, and then the system calls updateConstraints. The constraint-based layout system uses this return value to decide whether it needs to be called updateConstraints as part of the normal layout process.

If I toggle the in-call status bar off, the subview's layoutSubviews is not called at all, even though the main view does animate its resize. I have autoresizesSubviews set to NO for my custom view.

And in Interface Builder I have the top and bottom struts and the vertical arrow set. I had a similar question, but wasn't satisfied with the answer or any I could find on the net , so I tried it in practice and here is what I got:. I found that layoutSubviews: will be called on a view if and only if:. Your application forces layout to occur by calling the setNeedsLayout or layoutIfNeeded method of a view.

Some of the points in BadPirate's answer are only partially true:. It depends on the view's target view autoresize mask. If it has autoresize mask ON, layoutSubview will be called on each addSubview. If it has no autoresize mask then layoutSubview will be called only when the view's target View frame size changes.

Example: if you created UIView programmatically it has no autoresize mask by default , LayoutSubview will be called only when UIView frame changes not on every addSubview. Rotating a device only calls layoutSubview on the parent view the responding viewController's primary view.

This can be true only when your VC is in the VC hierarchy root at window. Therefore its view would not get noticed by calling layoutSubviews. I tracked the solution down to Interface Builder's insistence that springs cannot be changed on a view that has the simulated screen elements turned on status bar, etc.

Since the springs were off for the main view, that view could not change size and hence was scrolled down in its entirety when the in-call bar appeared. Turning the simulated features off, then resizing the view and setting the springs correctly caused the animation to occur and my method to be called.

An extra problem in debugging this is that the simulator quits the app when the in-call status is toggled via the menu. The documentation snippet is below. Does the animation work if you call this method explicitly during the animation? After a lot of trial and error I finally opened MainWindow. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. When is layoutSubviews called? Ask Question.

Asked 12 years, 7 months ago. Active 5 months ago. Viewed k times. I have a custom view that's not getting layoutSubview messages during animation. Under what circumstances is layoutSubviews actually called? Another part of the puzzle is that the window must be made key: [window makeKeyAndVisible]; of else the subviews are not automatically resized. Steve Weller Steve Weller 4, 4 4 gold badges 20 20 silver badges 30 30 bronze badges. This step is independent of whether Auto Layout is enabled for a view or not.

Same as with the layout step, rendering happens top-down [1]. The key method here is drawRect. All changes like background color, adding subviews etc.

Method viewDidLayoutSubviews is the most important among all. It is called to notify view controller that its view has finished the Layout step, i. This is an opportunity to make changes to a view after it has laid out its subviews, but before it becomes visible on the screen.

Intrinsic content size is a natural size of a view based on its content. Here are two tricks that will help you to simplify the layout and reduce the number of constraints:. Alignment rectangles are used by the Auto Layout engine to position views, thus separating their frames from the content being layed out.

To get a better control over the alignment rectangle , you can override alignmentRect forFrame: and frame forAlignmentRect:. These two methods must be an inverse of each other. The circles above are both image views that are centered in the same way by means of Auto Layout. The only difference is how the shadow is added to the circle. The shadow of the left circle is a part of its image, i. The right view has a shadow added by means of UIKit and its image contains only a circle.

The key to understanding their positioning is the difference in alignment rectangles. Thus, the center of alignment rectangle does not match with the circle center. Conversely, the right view has a shadow added by means of UIKit.



0コメント

  • 1000 / 1000