Several layout methods of UIView

Source: Internet
Author: User

IOS Layout Related methods: 1,layoutsubviews2,layoutifneeded3,setneedslayout4,setneedsdisplay5,drawrect6,sizethatfits7, SizeToFit probably have the above several. One, layoutsubviews this method, the default does not do anything, requires subclasses to rewrite. Layoutsubviews is called when 1,init initialization does not trigger Layoutsubviews, and is called when a frame that is not Cgrectzero is set. 2,addsubview will trigger layoutsubviews. 3, when the frame of the view is set, it will be called, of course, if the frame value has changed before and after the setting. 4, scrolling a uiscrollview will trigger the Layoutsubviews event. 5, rotating screen will trigger the layoutsubviews on the parent uiview. 6, changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView. 7, call Setlayoutsubviews directly.

Calling the Layoutsubviews method directly is not recommended.    Two, setneedslayout is marked as needing to be re-layout, not immediately refreshed, but Layoutsubviews must be called at the right time.   Three, layoutifneeded if there is a refresh with layoutifneeded tag, call Layoutsubviews immediately to re-layout.   for a set of animations, no matter how many constraints change, just perform a layoutifneeded in the animation, and all will be animated. If some changes do not want to animate, perform layoutifneeded before the animation.   Four, DrawRect this method is used to redraw.  drawrect is called in the following cases: 1, if the rect size is not set at UIView initialization, the DrawRect is not automatically called. The DrawRect call is called after the Controller->loadview,controller->viewdidload two methods, so don't worry about the controller, these view drawrect begin to draw. This allows you to set some values in the controller to the view (if the view requires some variable values at draw). 2, the method is called after SizeToFit, so you can call SizeToFit to calculate the size, then the system automatically calls the Drarect method. 3, by setting the Contentmode property value to Uiviewconentmoderedraw. The DrawRect method is called automatically each time the frame is set or changed. 4, call Setneedsdisplay directly, live setneedsdisplayinrect: Triggers the DrawRect method, but the precondition is that rect cannot be 0. Above 1, 2 recommended, 3,4 not recommended.  drawrect method Use note points: 1, if you use the UIView drawing, you can only get the corresponding contextref and drawing in the DrawRect method. If obtained in other methods, it gets to a invalidate ref and is not available for drawing. DrawRect cannot manually display the call, and it must be called setneedsdisplay or Setneedsdisplayinrect to have the system call the method automatically. 2, if using Calayer drawing, can only be drawn in the Drawincontext (similar to drawrect), or in the corresponding method in delegate, the same is called SETneeddisplay and so on indirectly call the above method. 3, to paint in real time, cannot use Gesturerecognizer, can only use Touchbegan and other methods to call Setneedsdisplay live refresh screen.   Five, Sizetofit1,sizetofit automatically calls the Sizethatfits method. 2,sizetofit should not be overridden in subclasses, the sizethatfits3,sizethatfits passed parameter should be overridden to be the current size of receiver, returning a suitable size. 4,sizetofit can be called directly by hand, SizeToFit and Sizethatfits methods are not recursive, not responsible for subviews, only responsible for their own.    add: 1,layoutsubviews subviews re-layout 2,layoutsubviews method call takes precedence over DRAWRECT3, Setneedslayout a tag that needs to be re-laid on the receiver tag, automatically calls the Layoutsubviews4,layoutifneeded method, such as its name, in the next cycle of the system Runloop Uikit will determine if the receiver needs layout. The 5,layoutifneeded traverse is not the superviews chain, but the subviews chain. 6,drawrect is the re-painting of receiver, can get Context7,setneedsdisplay on receiver labeled a need to be redrawn mark, write a draw period automatically draw. IPhone device refresh rate is 60z, that is, 1/60s after the redraw. 8,uiview Setneedsdisplay and Setneedslayout methods differ:      First, both of these methods are called asynchronously.       Next,           Setneedsdisplay will automatically call the DrawRect method, so that you can get uigraphicsgetcurrentcontext, you can draw.           setneedslayout will call Layoutsubvi by defaultEWS so that the view can be processed.       to sum up, setneedsdisplay convenient drawing, setneedslayout convenient data.  

Several layout methods for UIView

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.