When the layoutSubviews and drawRect methods of UIView are called, layoutsubviews are not called

Source: Internet
Author: User

When the layoutSubviews and drawRect methods of UIView are called, layoutsubviews are not called

First, both methods are executed asynchronously.LayoutSubviews facilitate data computing and drawRect facilitate view re-painting.

LayoutSubviews will be called in the following cases: 1. layoutSubviews will not be triggered during init initialization.
2. addSubview triggers layoutSubviews.
3. Setting the view Frame triggers layoutSubviews. Of course, the premise is that the frame value has changed before and after setting.
4. Rolling a UIScrollView triggers 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. 7. Call setLayoutSubviews directly. DrawRect is called in the following cases: 1. If the rect size is not set during UIView initialization, The drawRect is not 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.
The preceding 1, 2, and 3 do not advocate the drawRect method. Note: 1. If UIView is used for plotting, the corresponding contextRef can only be obtained and drawn in the drawRect: method. 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 calayer is used for plotting, it can only be drawn in drawInContext: (similar to drawRect) or in the corresponding method of delegate. The preceding method is also called indirectly, such as setNeedDisplay.
3. To draw images in real time, you cannot use gestureRecognizer. You can only use methods such as touchbegan to use setNeedsDisplay to refresh the screen in real time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.