iOS Development Basics-Fragmentation 18

Source: Internet
Author: User

1:Understanding of the initWithFrame method

1what is the. initWithFrame method? The initWithFrame method is used to initialize and return a new View object, based on the specified cgrect (size). Of course, other UI objects, there are initwithframe methods, but we use UIView as an example to figure out the initWithFrame method. 2. When do I use the initWithFrame method? Simply put, we use the initWithFrame method to programmatically declare that the UIView object is created. Here, we have to figure out two ways to initialize the UIView. A. Use Interface Builder mode. In this way, the nib file is used. Usually we say "drag the controls" in the way. In actual programming, we created the UIView object in interface Builder mode. (That is, by dragging the control), the initWithFrame method method is not called. Because the nib file already knows how to initialize the view. (Because, when we drag the view, we define properties such as length, width, background, etc.). At this point, the Initwithcoder method is called, and we can use the Initwithcoder method to redefine the properties we have set in the nib. B. Use the programming method. Is that we declare a subclass of UIView and do the "manual" Way of writing code. In practical programming, we use a programmatic approach to create a uiview or to create a uiview subclass. At this point, the initWithFrame method is called to instantiate the UIView. It is particularly important to note that if you overload the initWithFrame method in a subclass, you must first call the initWithFrame method of the parent class. Initializes the custom UIView subclass. For example:-(ID) initWithFrame: (cgrect) frame{ Self= [Super Initwithframe:frame];//call the initWithFrame method of the parent class firstif(self) {//You can then customize the initialization of this class (the UIView subclass). _scrollview =[[Uiscrollview alloc] initWithFrame:self.bounds]; [_scrollview Setframe:cgrectmake (0,0, the,480)]; _scrollview.contentsize= Cgsizemake ( the*3,480);    [Self addsubview:_scrollview]; }    returnSelf ;}

2:layoutsubviews Summary

 layoutsubviews is called in the following cases: A, init initialization does not trigger layoutsubviews    When initialized with initwithframe , however, when the value of rect is not Cgrectzero, B is triggered, and addsubview triggers LAYOUTSUBVIEWSC, Setting the frame of the view will trigger Layoutsubviews, of course, if the frame's value is changed before and after D, scrolling a uiscrollview will trigger Layoutsubviewse, Rotating screen triggers the Layoutsubviews event F on the parent uiview, changing the size of a uiview also triggers the Layoutsubviews event layoutsubviews on the parent UIView, We need to call when we are adjusting the child view position inside a class. In turn, this means: If you want to set the Subviews location externally, do not rewrite it. Layoutsubviews Subviews re-layout, Layoutsubviews method call refreshes the child object layout --setneedslayout method: Marked for need of re-layout, The asynchronous call layoutifneeded refreshes the layout without immediate refresh, but Layoutsubviews must be called -layoutifneeded method: If, There are tags that need to be refreshed, call Layoutsubviews immediately for layout (if no tags are not called layoutsubviews) if you want to refresh immediately, call [View setneedslayout] First, set the tag to the layout you want, Then call [view layoutifneeded] immediately, implement the layout before the first display of the view, the tag always "need to refresh", you can directly call [view layoutifneeded]  

3: When the cell row has other controls, the row is selected with respect to the control highlighting problem

here is the cell in the Accessoryview put a UIButton, the row is selected, UIButton is also highlighted in the selected strong state, through the following treatment can solve the problem @interface Ucaitableviewcell:uitableviewcell@end@implementation ucaitableviewcell@synthesize piosaDelegate=_piosadelegate;- (void) sethighlighted: (BOOL) highlighted animated: (BOOL) animated{[Super sethighlighted:highlighted animated:animated        ]; if(highlighted) {[(UIButton*) Self.accessoryview Sethighlighted:no]; }}- (void) setselected: (BOOL) selected animated: (BOOL) animated{[Super setselected:selected animated:animated]; if(selected) {[(UIButton*) Self.accessoryview Sethighlighted:no]; }}

4:uibutton Highlight Effect Removal

inherit UIButton then you can rewrite the Sethighlighed method, and nothing is written;. h file: #import<UIKit/UIKit.h>@interface hwemotiontabbarbutton:uibutton@end.m file: #import"HWEmotionTabBarButton.h"@implementation Hwemotiontabbarbutton-(ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {//Set Text color[self Settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal];        [Self Settitlecolor:[uicolor darkgraycolor] forstate:uicontrolstatedisabled]; //Set FontSelf.titleLabel.font = [Uifont systemfontofsize: -]; }    returnSelf ;}- (void) sethighlighted: (BOOL) highlighted {//button highlighting everything that's been done is gone.} @end

5: Encapsulation of one Tab

. H file content #import<UIKit/UIKit.h>typedefenum{hwemotiontabbarbuttontyperecent,//recentHwemotiontabbarbuttontypedefault,//defaultHwemotiontabbarbuttontypeemoji,//emojiHwemotiontabbarbuttontypelxh,//The Little Flower of the wave} hwemotiontabbarbuttontype; @class hwemotiontabbar; @protocol hwemotiontabbardelegate<NSObject>@optional- (void) Emotiontabbar: (Hwemotiontabbar *) TabBar Didselectbutton: (hwemotiontabbarbuttontype) ButtonType; @end @interface Hwemotiontabbar:uiview@property (nonatomic, weak) ID<HWEmotionTabBarDelegate>Delegate; @end Note: This is mainly to introduce @class. M file contents: @interface Hwemotiontabbar () @property (nonatomic, weak) Hwemotiontabbarbutton*selectedbtn, @end @implementation Hwemotiontabbar-(ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[Self setupbtn:@"recent"Buttontype:hwemotiontabbarbuttontyperecent]; [Self setupbtn:@"default"Buttontype:hwemotiontabbarbuttontypedefault];//[self btnclick:[self setupbtn:@ "default" Buttontype:hwemotiontabbarbuttontypedefault]];[Self setupbtn:@"Emoji"Buttontype:hwemotiontabbarbuttontypeemoji]; [Self setupbtn:@"The Little Flower of the wave"Buttontype:hwemotiontabbarbuttontypelxh]; }    returnSelf ;}/** * Create a button * * @param title button text*/-(Hwemotiontabbarbutton *) SETUPBTN: (NSString *) title ButtonType: (hwemotiontabbarbuttontype) buttontype{//Create buttonHwemotiontabbarbutton *btn =[[Hwemotiontabbarbutton alloc] init];    [Btn addtarget:self Action: @selector (Btnclick:) Forcontrolevents:uicontroleventtouchdown]; Btn.tag=ButtonType;    [Btn Settitle:title Forstate:uicontrolstatenormal];        [Self addsubview:btn]; //set a background pictureNSString *image =@"Compose_emotion_table_mid_normal"; NSString*selectimage =@"compose_emotion_table_mid_selected"; if(Self.subviews.count = =1) {Image=@"Compose_emotion_table_left_normal"; Selectimage=@"compose_emotion_table_left_selected"; } Else if(Self.subviews.count = =4) {Image=@"Compose_emotion_table_right_normal"; Selectimage=@"compose_emotion_table_right_selected";    } [btn Setbackgroundimage:[uiimage imagenamed:image] forstate:uicontrolstatenormal];        [btn Setbackgroundimage:[uiimage imagenamed:selectimage] forstate:uicontrolstatedisabled]; returnbtn;}- (void) layoutsubviews{[Super Layoutsubviews]; //set the frame of the buttonNsuinteger Btncount =Self.subviews.count; CGFloat BTNW= Self.width/Btncount; CGFloat BTNH=Self.height;  for(inti =0; i<btncount; i++) {Hwemotiontabbarbutton*BTN =Self.subviews[i]; BTN.Y=0; Btn.width=BTNW; Btn.x= i *BTNW; Btn.height=BTNH; }}- (void) Setdelegate: (idDelegate{_delegate=Delegate; //Check the "Default" button[Self Btnclick: (Hwemotiontabbarbutton *) [self viewwithtag:hwemotiontabbarbuttontypedefault];}/** * button click*/- (void) Btnclick: (Hwemotiontabbarbutton *) btn{//Convert the selected effectself.selectedBtn.enabled =YES; Btn.enabled=NO; SELF.SELECTEDBTN=btn; //Notification Agent    if([Self.Delegaterespondstoselector: @selector (Emotiontabbar:didselectbutton:)]) {[Self.Delegateemotiontabbar:self DidSelectButton:btn.tag]; }} @end Note: When the control is added, Self.subviews.count this value is starting from 1, and setdelegate is used to display the default is selected, because there is an enumeration so can be very accurate to set the default of the UIButton, Uicontrolstatedisabled This state is so that when it is selected, it cannot be clicked again, with the enabled setting, a property created in the code is used to hold the currently selected UIButton, which is converted to the value in the event Btnclick;

iOS Development Basics-Fragmentation 18

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.