Depending on the action you need to perform, here are a few ways:
-(id)initWithFrame:(CGRect)frame
-UIView the specified initialization method; Always send to UIView to initialize, unless it is loaded from a nib file;
-(ID) Initwithcoder: (Nscoder *) coder
-Send this message to UIView when loaded from the nib file;
-(void)awakeFromNib
-This message will be sent after initialization and connection of all the objects in the nib; Applies only to loading objects from the nib; If you want to override it, you must also call the parent class'sawakeFromNib
;
-(void) Willmovetosuperview: (UIView *) Newsuperview
- This message is sent when a child view is about to be added to another view;
-(void) Willmovetowindow: (UIWindow *) NewWindow
- Sent when a view (or its hyper-view) is about to be added to the window;
-(void) Didmovetowindow
-sends this message when the view obtains its set of window properties.
Basically, you can choose: the actions performed during the initialization of your view (1 & 2), when loading from the nib file (3), before inserting to the view level (4 & 5) and after (6 & 7)
Layoutsubviews convenient data calculation, DrawRect convenient view redraw.
The Layoutsubviews is called in the following cases:
1. Init initialization does not trigger layoutsubviews.
2, Addsubview will trigger layoutsubviews.
3, set the frame of the view will trigger Layoutsubviews, of course, if the frame value has changed before and after the setting.
4, rolling a uiscrollview will trigger layoutsubviews.
5. Rotating screen will trigger the Layoutsubviews event on the parent UIView.
6, changing the size of a uiview will also trigger the parent UIView on the Layoutsubviews event. 7, call Setlayoutsubviews directly.
The DrawRect is called in the following cases:
1. If the rect size is not set at UIView initialization, the drawrect will not be automatically called. the DrawRect is dropped after the Controller->loadview, Controller->viewdidload two method. So don't worry. In the controller, The drawrect of the view began to draw. This allows you to set some values in the controller to the view (if some variable values are needed for these view draw).
2, the method is called Sizet o Fit is called, so you can first call SizeToFit to calculate the size. The system then automatically calls the DrawRect: method.
3, by setting the Contentmode property value to Uiviewcontentmoderedraw. The drawrect will be called automatically each time the frame is set or changed:.
4. Call Setneedsdisplay directly, or setneedsdisplayinrect: Trigger DrawRect:, but there is a precondition that rect cannot be 0.
above 1, 2 recommended; 3,4 does not advocate
DrawRect methods use note points:
1, if using UIView drawing, only in the DrawRect: method to obtain the corresponding Contextref and drawing. If obtained in other methods, it gets to a invalidate ref and cannot be used for paint. DrawRect: The method cannot manually display the call, and the system must be automatically tuned by calling Setneedsdisplay or Setneedsdisplayinrect.
2, if using Calayer drawing, can only be drawn in Drawincontext: (similar to fish drawrect), or in the corresponding method of delegate. Also call Setneeddisplay and so on indirectly call the above method
3, to real-time paint, can not use Gesturerecognizer, can only use Touchbegan and other methods to drop the Setneedsdisplay real-time refresh screen
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UIView (including subclasses) the timing of performing actions on several initializations