IOS-setNeedsLayout and other layout Methods
List the related methods of iOS layout:
LayoutSubviews
LayoutIfNeeded
SetNeedsLayout
SetNeedsDisplay
DrawRect
SizeThatFits
SizeToFit
Generally, the above several items are commonly used. The specific items should be different.
LayoutSubviews
By default, this method does not do anything and must be rewritten by subclass. The system will call this method in many cases:
1. layoutSubviews will not be triggered during initialization, but will be triggered if the frame is not set to CGRectZero.
2. addSubview triggers layoutSubviews
3. Setting the view Frame will trigger layoutSubviews. Of course, the premise is that the frame value has changed before and after it is set.
4. Rolling a UIScrollView will trigger layoutSubviews
5. Rotating Screen triggers the layoutSubviews event on the parent UIView.
6. Changing the size of a UIView triggers the layoutSubviews event on the parent UIView.
In Apple's official document, You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want. layoutSubviews, which must be called when the position of the subview is adjusted within a class. This means that if you want to set the location of the subviews externally, do not rewrite it.
SetNeedsLayout
The layoutSubviews will be called if it is marked as needing to be re-deployed and not refreshed immediately.
Use layoutIfNeeded to update immediately
LayoutIfNeeded
If there are tags that need to be refreshed, call layoutSubviews to layout now
This animation is useful for example.
There is a label on it and a button in the middle. The label is automatically displayed in the upper left corner. Then our left constraint
@ IBOutlet weak var leftContrain: NSLayoutConstraint!
Declare it in viewDidLoad, and connect it in Main. storyboard. When you click the button, we change the left distance to 100.
Add this sentence to the button click event.
LeftContrain. constant = 100
Then we want an animation effect.
If you do this
UIView. animateWithDuration (0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions. AllowAnimatedContent, animations :{
Self. leftContrain. constant = 100
}, Completion: nil)
You will find it. In fact, this sentence self. leftContrain. constant = 100 only executes setNeedsLayout to mark the need for re-layout, but it is not executed immediately. So we need to call this method in animation layoutIfNeeded
So the code should be written like this.
LeftContrain. constant = 100
UIView. animateWithDuration (0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions. AllowAnimatedContent, animations :{
Self. view. layoutIfNeeded () // implement layout now
}, Completion: nil)
Therefore, no matter how many constraints are written, you only need to use self. view. layoutIfNeeded () in the animation, all of which will be animated. If some changes do not require animation. Execute self. view. layoutIfNeeded () before the animation ()
DrawRect
This method is used for re-painting.
DrawRect is called in the following cases:
1. If the rect size is not set during UIView initialization, The drawRect will not be automatically called. DrawRect is called after the Controller-> loadView, Controller-> viewDidLoad method. so don't worry about the drawRect of these views in the controller. in this way, you can set some values for the View in the Controller (if some variable values are required for the View draw ).
2. This method is called after sizeToFit is called. Therefore, you can call sizeToFit to calculate the size. Then the system automatically calls drawRect: method. 3. Set the value of contentMode to UIViewContentModeRedraw. The drawRect: is automatically called every time the frame is set or changed :. 4. Call setNeedsDisplay directly or setNeedsDisplayInRect to trigger drawRect:, but there is a precondition that the rect cannot be 0. 1, 2 and 3 are not recommended.
Note the following when using the drawRect method:
1. If you use UIView for plotting, you can only obtain the corresponding contextRef in the drawRect: method and plot it. If it is obtained in other methods, an invalidate ref will be obtained and cannot be used for drawing. DrawRect: The call method cannot be displayed manually. You must call setNeedsDisplay or setNeedsDisplayInRect to enable the system to automatically call this method. 2. If you use calayer to draw a chart, you can only draw it in drawInContext: (similar to drawRect) or the corresponding method in delegate. It also calls setNeedDisplay and other indirect call of the above method. 3. If you want to draw images in real time, you cannot use gestureRecognizer. You can only use touchbegan or other methods to use setNeedsDisplay to refresh the screen in real time.
SizeToFit
SizeToFit will automatically call the sizeThatFits method;
SizeToFit should not be overwritten in the subclass, and should be rewritten in sizeThatFits
The input parameter sizeThatFits is the current size of the aggreger, and an appropriate size is returned.
SizeToFit can be manually called directly by sizeToFit and sizeThatFits methods without recursion. It is not responsible for subviews and is only responsible for itself.