#import "ViewController.h" #import "CGTabbar.h" #import "QHCListView.h" #import "QHJavaListView.h" #import " QHOCListView.h "@interface Viewcontroller () <CGTabbarDelegate> @property (nonatomic,strong) Nsarray *books;@ Property (Nonatomic,weak) Qhoclistview *oclistview; @property (nonatomic,weak) Qhjavalistview *javalistview; @property (Nonatomic,weak) Qhclistview *clistview; @end @implementation viewcontroller-(Qhoclistview *) oclistview{if (_oclistview = = nil) { Qhoclistview *oclistview = [Qhoclistview Oclistview]; [Self.view Addsubview:oclistview]; _oclistview = Oclistview; } return _oclistview;} -(Qhjavalistview *) javalistview{if (_javalistview = = nil) {Qhjavalistview *javalistview = [Qhjavalistview java ListView]; [Self.view Addsubview:javalistview]; _javalistview = Javalistview; NSLog (@ "+++++++++++"); } return _javalistview;} -(Qhclistview *) clistview{if (_clistview = = nil) {Qhclistview *clistview = [Qhclistview cListView]; [Self.view Addsubview:clistview]; _clistview = CListView; } return _clistview;} -(void) viewdidload {[Super viewdidload]; NSString *path = [[NSBundle mainbundle]pathforresource:@ "Books.plist" oftype:nil]; _books = [Nsarray Arraywithcontentsoffile:path]; NSLog (@ "%@", _books); Cgtabbar *tabbar = [Cgtabbar Tabbar]; [Self.view Addsubview:tabbar]; UIButton *btn1 = [UIButton buttonwithtype:uibuttontypecustom]; UIButton *btn2 = [UIButton buttonwithtype:uibuttontypecustom]; UIButton *btn3 = [UIButton buttonwithtype:uibuttontypecustom]; [Self configbutton:btn1 andtitile:@ "OC" andimagename:@ "Upomp_button_keyboard3" andselectedimagename:@ "Upomp_button _keyboard3_highlighted "]; [Self configbutton:btn2 andtitile:@ "Java" andimagename:@ "Upomp_button_keyboard3" andselectedimagename:@ "Upomp_" Button_keyboard3_highlighted "]; [Self configbutton:btn3 andtitile:@ "C" andimagename:@ "Upomp_button_keyboard3" Andselectedimagename:@ "upomp_button_keyboard3_highlighted"]; Tabbar.delegate = self; Tabbar.items = @[btn1,btn2,btn3]; NSLog (@ "%@", _books); Do any additional setup after loading the view, typically from a nib.} -(void) Configbutton: (UIButton *) btn Andtitile: (NSString *) title Andimagename: (NSString *) imageName Andselectedimagename: (NSString *) selectedimagename{[btn Settitle:title forstate:uicontrolstatenormal]; [btn Setbackgroundimage:[uiimage Imagenamed:imagename] forstate:uicontrolstatenormal]; [btn Setbackgroundimage:[uiimage Imagenamed:selectedimagename] forstate:uicontrolstateselected];} -(void) tabbaritemselected: (Cgtabbar *) Tabbar Anditem: (UIButton *) Item Andindex: (Nsinteger) index{if (index==0) { [Self.view BringSubviewToFront:self.ocListView]; NSLog (@ "--------------"); } else if (index ==1) {[Self.view BringSubviewToFront:self.javaListView]; } else if (index ==2) {[Self.view BringSubviewToFront:self.cListView]; } [Self.view Bringsubviewtofront:tabbar];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
#import <UIKit/UIKit.h> @interface qhoclistview:uiview+ (id) oclistview; @property (nonatomic,strong) Nsarray * Books; @end
#import "QHOCListView.h" @implementation qhoclistview+ (ID) oclistview{return [[Self alloc]init];} -(void) Willmovetosuperview: (UIView *) newsuperview{nsstring *path = [[NSBundle mainbundle]pathforresource:@] Books.plist "Oftype:nil"; _books = [Nsarray Arraywithcontentsoffile:path]; NSLog (@ "%@", _books); Self.frame = Newsuperview.bounds; Self.backgroundcolor = [Uicolor Redcolor]; int totalcolumns = 3; The size of the application cgfloat BOOKW = 90; CGFloat Bookh = 125; Clearance = (width of view-3 * application width)/4 cgfloat Marginx = (Self.frame.size.width-totalcolumns * bookw)/(Totalcolumns + 1); CGFloat marginy = 15; Create a corresponding box (index) for (int index = 0; index<self.books.count; index++) {//create UIView *bookview =) According to the number of applications [[UIView alloc] init]; Set background Color Bookview.backgroundcolor = [Uicolor Cyancolor]; Calculate the position of the box//Calculate line number and column number int row = Index/totalcolumns; int col = index% Totalcolumns; // calculates x and y cgfloat bookx = marginx + col * (bookw + Marginx); CGFloat Booky = + Row * (Bookh + marginy); Set Frame Bookview.frame = CGRectMake (Bookx, Booky, BOOKW, Bookh); Add a frame to the Controller view [self Addsubview:bookview]; Add control//index position corresponding application information nsdictionary *bookinfo = Self.books[index]; Add picture//uiimageview *iconview = [[Uiimageview alloc] init]; UIButton * btn = [UIButton buttonwithtype:uibuttontypecustom]; [Btn setbackgroundimage:[uiimage imagenamed:bookinfo[@ "icon"] forstate:uicontrolstatenormal]; Set position cgfloat iconw = 60; CGFloat iconh = 80; CGFloat IconX = (bookw-iconw) * 0.5; CGFloat icony = 0; Btn.frame = CGRectMake (IconX, Icony, Iconw, Iconh); [Bookview ADDSUBVIEW:BTN]; Add name UILabel *namelabel = [[UILabel alloc] init]; Set position cgfloat Namew = bookw; CGFloat Nameh = 20; CGFloat NameX = 0; CGFloat Namey = Icony + iconh; Namelabel.frame = CGRectMake (NameX, Namey, Namew, Nameh); Set Text Namelabel.text = bookinfo[@ "name"]; Set Font namelabel.font = [Uifont systemfontofsize:13]; Set Text Center alignment namelabel.textalignment = nstextalignmentcenter; [Bookview Addsubview:namelabel]; Add Download button UIButton *downloadbtn = [[UIButton alloc] init]; Set position cgfloat DOWNLOADX = 12; CGFloat Downloady = Namey + Nameh; CGFloat DOWNLOADW = bookW-2 * DOWNLOADX; CGFloat downloadh = 20; Downloadbtn.frame = CGRectMake (DOWNLOADX, Downloady, DOWNLOADW, downloadh); Set the default background UIImage *normalimage = [UIImage imagenamed:@ "Upomp_button_keyboard3"]; [Downloadbtn setbackgroundimage:normalimage Forstate:uicontrolstatenormal]; Set the highlighted background UIImage *highimage = [UIImage imagenamed:@ "upomp_button_keyboard3_highlighted"]; [Downloadbtn setbackgroundimage:highimage forstate:uicontrolstatehighlighted]; Set the text of the button [downloadbtn settitle:@ "Download" forstate:uicontrolstatenormal]; Sets the font of the button text DownloadBtn.titleLabel.font = [Uifont systemfontofsize:13]; [Bookview ADDSUBVIEW:DOWNLOADBTN]; }} @end
#import <UIKit/UIKit.h> @class cgtabbar; @protocol cgtabbardelegate <nsobject>-(void) tabbaritemselected :(Cgtabbar *) Tabbar Anditem: (UIButton *) Item Andindex: (Nsinteger) index; @end @interface Cgtabbar:uiview@property ( Nonatomic,strong) Nsarray *items; @property (nonatomic,assign) id<cgtabbardelegate>delegate;+ (id) tabbar; @end
#import "CGTabbar.h" @implementation cgtabbar+ (ID) tabbar{return [[Self alloc]init];} -(ID) initWithFrame: (CGRect) frame{if (self =[super initwithframe:frame]) {} return to self; -(void) Btntouch: (UIButton *) btn{if (btn.selected) return; For (UIButton *tmp in self.items) {tmp.selected = NO; } btn.selected = YES; [_delegate tabbaritemselected:self anditem:btn AndIndex:btn.tag];} -(void) Setitems: (Nsarray *) items{_items =items; CGFloat BTNW = Self.frame.size.width/items.count; for (int i = 0; i < Items.Count; i++) {UIButton *btn = items[i]; [Self addsubview:btn]; CGFloat btnx = I*BTNW; CGFloat btny = 0; CGFloat BTNH = self.frame.size.height; Btn.frame = CGRectMake (Btnx, Btny, BTNW, BTNH); [Btn addtarget:self Action: @selector (Btntouch:) forcontrolevents:uicontroleventtouchupinside]; Btn.tag = i; [Self btntouch:items[0]]; }}-(void) Willmovetosuperview: (UIView *) Newsuperview{cgfloat SELFW = newSuperview.bounds.size.width; CGFloat SELFH = 49; CGFloat selfx = 0; CGFloat selfy = NEWSUPERVIEW.BOUNDS.SIZE.HEIGHT-SELFH; Self.frame = CGRectMake (Selfx, Selfy, SELFW, SELFH); Self.backgroundcolor = [Uicolor graycolor];} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-ui-customizing Tabbar book layouts