There is a method in UIView layoutSubviews , what is the specific function of this method?
- (void)layoutSubviews; // override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
Official Document explanation
Let's take a look at an explanation of Apple's official documentation:
The default implementation of this method does nothing on IOS 5.1 and earlier. Otherwise, the default implementation uses any constraints that has set to determine the size and position of any subviews . Subclasses can override this method as needed to perform more precise layout of their subviews. Should override this method only if the autoresizing and constraint-based behaviors of the subviews does not offer the B Ehavior you want. You can use your implementation to set the frame rectangles of your subviews directly. You should is not a call this method directly. If you want-to-force a layout update, call the Setneedslayout method instead to does so prior to the next drawing update. If you want to update the layout of your views immediately, call the Layoutifneeded method.
The last paragraph says, do not call this method directly. If you want to force the layout to be updated, you can call setNeedsLayout the method, and if you want to immediately count your views , you need to call the layoutIfNeeded method.
Layoutsubviews effect
layoutsubviews is to subviews re-layout. For example, when we want to update the location of the sub-view, we can call
layoutSubviewsThe default is not to do anything, when used, need to be rewritten in self-thunder.
Layoutsubviews The following conditions will be called
The official Apple document has emphasized that you cannot directly invoke layoutsubviews under what circumstances will it be called? Through Baidu Search, found the following several situations layoutsubviews will be called.
- called Directly
setLayoutSubviews . (This is stated in the official Apple document above)
addSubviewThe time.
- When
view the frame change occurs.
UIScrollViewwhen it's sliding.
- rotation
screen will trigger the parent uiview " FONT-SIZE:0.85EM; Font-family:consolas,inconsolata,courier,monospace; margin:0px 0.15em; padding:0px 0.3em; White-space:pre-wrap; border:1px solid RGB (234,234,234); Background-color:rgb (248,248,248); Display:inline ">layoutsubviews event.
- Changing a
UIView size also triggers the UIView event on the parent layoutSubviews .
I tested it briefly, and it's basically going to be called. Attention:
layoutSubviewsThe method is very convenient when laying out the self-thunder view. You can do it yourself and get a deeper understanding layoutSubviews of the invocation mechanism.
?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The role and invocation mechanism of iOS development layoutsubviews