iOS development Project-05 Theme settings

Source: Internet
Author: User

iOS development Project-05 Theme settings

First, the realization effect

1. Show

Note the navigation bar of the viewing interface

"Write message" on the navigation bar of the message interface

"System Settings" on the Discovery Interface

"I" Interface on the German "settings"

2. Implementation Notes

(1) suitable for IOS6 and IOS7, requires the navigation title bar and the above button settings are basically the same.

(2) The navigation bar on the German button, set three states, the default state is orange, the unavailable state is highlighted gray, click (highlight) the status of Red.

(3) To set the buttons on the navigation bar, there are two ways

The first: The following is a message page that adds "write private messages" code that can be copied every time a similar setting is required.

1     //The first method, this method is a disadvantage of 2/     * 3      UIButton *button = [UIButton buttonwithtype:uibuttontypecustom]; 4      [button settitle:@ "Write private Messages" forstate:uicontrolstatenormal]; 5      [button Settitlecolor:[uicolor Orangecolor] forstate:uicontrolstatenormal]; 6      [Button settitlecolor:[ Uicolor Redcolor] forstate:uicontrolstatehighlighted]; 7      [button Settitlecolor:[uicolor Lightgraycolor] forstate:uicontrolstatedisabled]; 8      Button.titleLabel.font = [Uifont systemfontofsize:15]; 9      //Set the size of the button text to the button's own size of ten      button.size = [Button.currenttitle sizewithfont:button.titlelabel.font];11      /      /Monitor button click on      self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] Initwithcustomview: button];14      self.navigationItem.rightBarButtonItem.enabled = no;15      */

The second type (recommended-set its theme):

 1/** 2 * Set Uibarbuttonitem Theme 3 */4 + (void) Setupbarbuttonitemtheme 5 {6//By setting appearance object, be able to modify all Uibarbutto in entire project Nitem Style 7 Uibarbuttonitem *appearance=[uibarbuttonitem appearance];     8 9//Set the text properties of 10//1. Set the properties of text in normal state one by one nsmutabledictionary *textattrs=[nsmutabledictionary dictionary];12 Set font Textattrs[uitextattributefont]=[uifont systemfontofsize:15];14//This is an offset of 015 Textattrs[uitextattribute Textshadowoffset]=[nsvalue valuewithuioffset:uioffsetzero];16//Set color to orange Textattrs[uitextattributetextcolor]=[ui     Color orangecolor];18 [Appearance settitletextattributes:textattrs forstate:uicontrolstatenormal];19 20 21 2. Set the properties of the text in the highlighted State 22//Use the Textattrs in 1 for common settings nsmutabledictionary *hightextattrs=[nsmutabledictionary dictionarywit HDICTIONARY:TEXTATTRS];24//Set color to red Hightextattrs[uitextattributetextcolor]=[uicolor redcolor];26 [Appearanc E Settitletextattributes:hightextattrs forstate:uicontrolstatehighlighted]; 27 28 29//3. Set the properties of the text in the unavailable State 30//Use the Textattrs in 1 for general settings Nsmutabledictionary *DISABLETEXTATTRS=[NSMUTABL Edictionary dictionarywithdictionary:textattrs];32//Set color to gray Disabletextattrs[uitextattributetextcolor]=[uicolo     R lightgraycolor];34 [appearance settitletextattributes:disabletextattrs forstate:uicontrolstatedisabled];35 36 Set background 37//tip: To make a button's background disappear, you can set a completely transparent background image [appearance setbackbuttonbackgroundimage:[uiimage imagewithname:@ "Navigationbar_button_background"]forstate:uicontrolstatenormal barmetrics:uibarmetricsdefault];39}

Second, the Code implementation

1. Set the code for the theme:

YYUINAVIGATIONVIEWCONTROLLER.M file

  1//2//YYUINAVIGATIONVIEWCONTROLLER.M 3//03-Weibo Add Navigation function 4//5 6 #import "YYNavigationViewController.h" 7 8 @interface Yynavigationviewcontroller () 9 @end @implementation Yynavigationviewcontroller 13 14/** 15 * First      This method is called once when the class is called * * * + (void) Initialize 18 {19//Set Uibarbuttonitem theme [self setupbarbuttonitemtheme]; 21 22//Set Uinavigationbar theme [self setupnavigationbartheme]; 24} 25 26/** 27 * Set Uinavigationbar Theme * * + (void) Setupnavigationbartheme 30 {31//By setting the Appearance object, the entire item can be modified The style of all uinavigationbar in the eye Uinavigationbar *appearance=[uinavigationbar appearance]; 33 34//Set the background of the navigation bar (!IOS7) {appearance setbackgroundimage:[uiimage imagewithname:@ "Navigatio Nbar_background "] forbarmetrics:uibarmetricsdefault]; 37} 38 39//Set Text Properties Nsmutabledictionary *textattrs=[nsmutabledictionary dictionary]; 41//Set font color Textattrs[uitextattributetextcolor]=[uicolor blAckcolor]; 43//Set font Textattrs[uitextattributefont]=[uifont BOLDSYSTEMFONTOFSIZE:20]; 45//Set the offset of the font (0) 46//Description: Uioffsetzero is a struct, only packaged into a Nsvalue object can be put into the dictionary textattrs[uitextattributetextshadowoffset]= [Nsvalue Valuewithuioffset:uioffsetzero]; [Appearance settitletextattributes:textattrs]; 49} 50 51/** 52 * Set the theme of Uibarbuttonitem * * * + (void) Setupbarbuttonitemtheme 55 {56//By setting the Appearance object, the entire item can be modified The style of all uibarbuttonitem in the eye uibarbuttonitem *appearance=[uibarbuttonitem appearance]; 58 59//Set the text properties of 60//1. Set the properties of text in normal state nsmutabledictionary *textattrs=[nsmutabledictionary dictionary]; 62//Set font Textattrs[uitextattributefont]=[uifont systemfontofsize:15]; 64//This is an offset of 0 textattrs[uitextattributetextshadowoffset]=[nsvalue Valuewithuioffset:uioffsetzero]; 66//Set color to orange textattrs[uitextattributetextcolor]=[uicolor Orangecolor]; [Appearance settitletextattributes:textattrs Forstate:uicontrolstatenormaL]; 69 70 71//2. Set the properties of the text in the highlighted State 72//Use the Textattrs in 1 for universal settings Nsmutabledictionary *hightextattrs=[nsmutable Dictionary Dictionarywithdictionary:textattrs]; 74//Set color to red Hightextattrs[uitextattributetextcolor]=[uicolor redcolor]; [Appearance settitletextattributes:hightextattrs forstate:uicontrolstatehighlighted]; 77 78 79//3. Set the properties of the text in an unavailable state 80//Use the Textattrs in 1 for general settings Bayi Nsmutabledictionary *disabletextattrs=[nsmut Abledictionary Dictionarywithdictionary:textattrs]; 82//Set color to Grey disabletextattrs[uitextattributetextcolor]=[uicolor lightgraycolor]; [Appearance settitletextattributes:disabletextattrs forstate:uicontrolstatedisabled]; 85 86//SET background 87//tip: To make a button's background disappear, you can set a completely transparent background image [appearance setbackbuttonbackgroundimage:[uiimage imagewithname:@ "Navigationbar_button_background"]forstate:uicontrolstatenormal BarMetrics:UIBarMetricsDefault]; 89} 90 91/** 92 * When the view of the navigation controller is created, call 93 */94-(void) Viewdidload ([Super Viewdidload]; 97} 98 99/**100 * Capable of intercepting all push-in sub-controllers 101 */102-(void) Pushviewcontro Ller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated103 {104//If the current push is not a stack top controller, then hide the Tabbar toolbar for a long time if (s elf.viewcontrollers.count>0) {106 viewcontroller.hidesbottombarwhenpushed=yes;107 108//intercept push The upper-left corner of the navigation bar and the upper-right corner button 109 Viewcontroller.navigationitem.leftbarbuttonitem=[uibarbuttonitem itemwithimagename:@ " Navigationbar_back "highimagename:@" navigationbar_back_highlighted "target:self Action: @selector (back)];110 ViewCo Ntroller.navigationitem.rightbarbuttonitem=[uibarbuttonitem itemwithimagename:@ "Navigationbar_more" highimagename:@ "navigationbar_more_highlighted" target:self Action: @selector (more)];111}113 [Super PU Shviewcontroller:viewcontroller animated:yes];114}115-(void) back117 {118 #warning here is self, Because self is the navigation controller that is currently in use 119 [self popviewcontrolleranimated:yes];120}121 122 -(void) more123 {124 [self poptorootviewcontrolleranimated:yes];125}126 127 @end 

Code for each interface setting:

Message interface, setting write message to unavailable state

YYMESSAGEVIEWCONTROLLER.M file

 1//2//YYMESSAGEVIEWCONTROLLER.M 3//4//5//6//7//8 9 #import "YYMessageViewController.h" @interface YY Messageviewcontroller () @end14 @implementation YYMessageViewController16-(ID) Initwithstyle: ( Uitableviewstyle) style18 {"Self" = [Super initwithstyle:style];20 if (self) {//Custom Initialization2     2}23 return self;24}25-(void) viewDidLoad27 {[Super viewdidload];29 30///First method, this method is flawed 31 /*32 UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];33 [Button settitle:@ ' write private messages ' Forstate:uicon trolstatenormal];34 [button Settitlecolor:[uicolor orangecolor] forstate:uicontrolstatenormal];35 [button setTit Lecolor:[uicolor Redcolor] forstate:uicontrolstatehighlighted];36 [button Settitlecolor:[uicolor LightGrayColor] for state:uicontrolstatedisabled];37 Button.titleLabel.font = [Uifont systemfontofsize:15];38//Set button text size to the button's own size Button.size = [Button.currenttitlE sizewithfont:button.titlelabel.font];40 41//Monitor button click on Self.navigationItem.rightBarButtonItem = [[Uibarbu      Ttonitem alloc] initwithcustomview:button];43 self.navigationItem.rightBarButtonItem.enabled = no;44 */45 46 The second method is Self.navigationitem.rightbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "write message" Style:UIBarButtonIt  Emstyledone target:self action:nil];48//set to unavailable status Self.navigationitem.rightbarbuttonitem.enabled=no;50}51 52 53 #pragma mark-table View Data source54-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger ) Section55 {5;57 return to}58-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (nsind Expath *) indexPath60 {nsstring *id = @ "cell"; UITableViewCell *cell = [TableView dequeuereusablecellwi thidentifier:id];63 if (!cell) {cells = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle   Reuseidentifier:id];65  }66 Cell.textLabel.text = [NSString stringwithformat:@ "%d----Message page test data", indexpath.row];67 return cell;68}69 70 -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexPath71 {72//click on the cell to jump to the next bounds Face Uiviewcontroller *NEWVC = [[Uiviewcontroller alloc] init];74 newVc.view.backgroundColor = [Uicolor redcolor];7 5 Newvc.title = @ "new controller"; [Self.navigationcontroller pushviewcontroller:newvc animated:yes];77}78 @end

Discover the interface, set the system settings, the left button is unavailable, the right button is the default state.

YYDISCOVERVIEWCONTROLLER.M file

1//2//  YYDISCOVERVIEWCONTROLLER.M 3//  02-Weibo add sub-controller and set project structure 4//5  6 #import "YYDiscoverViewController.h" 7< C5/>8 @interface Yydiscoverviewcontroller () 9 @end11 @implementation YYDiscoverViewController13-(void) viewdidl oad15 {     [super viewdidload];17     Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem alloc] initwithtitle:@ "System Setup" Style:uibarbuttonitemstyledone target:self action:nil];19     Self.navigationitem.rightbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "System settings" style: Uibarbuttonitemstyledone target:self action:nil];21     //Set Leftbarbuttonitem as non-     clickable Self.navigationitem.leftbarbuttonitem.enabled=no;24}25 @end

2. Code Description:

(1) Set the theme of the title on the navigation (bold, remove shadow • Set the offset to both horizontal and vertical deviations of 0, structure, only the packing scale Nsvalue object, in order to fit into the dictionary or array)

(2) Modify the button (only by setting its theme to complete, the first method is not complete)

(3) Tip: +initialize This class method is called once for the first time the class is used

3. Tips for setting up the navigation bar:

(1) Do the art of making a transparent picture

(2) or let the artist make a picture of the same background as the navigation bar

4. Refactoring the code (set text properties)

Using dictionarywithdictionary:

1     //2. Set the properties of the text in the highlighted State 2     //Use the Textattrs in 1 for common settings 3     nsmutabledictionary *hightextattrs=[nsmutabledictionary Dictionarywithdictionary:textattrs];4     //Set color to red 5     Hightextattrs[uitextattributetextcolor]=[uicolor Redcolor];6     [appearance settitletextattributes:hightextattrs forstate:uicontrolstatehighlighted];

New questions:

After switching to iOS7, the simulator has a shaded line in the lower corner of the navigation bar. is because iOS has 66-64 more pixels out of 2 pixel points. Remove the background of the navigation bar and you can resolve it.

When setting the navigation bar background, make a judgement, if it is iOS7 then do not set the navigation bar background.

1    //Set the navigation bar background 2     if (!IOS7) {3         [appearance setbackgroundimage:[uiimage imagewithname:@ "Navigationbar_ Background "] Forbarmetrics:uibarmetricsdefault];4     }

Third, important tips

Observe the following code:

 1/** 2 * Add a sub-controller 3 * 4 * @param CHILDVC Child Control Object 5 * @param title Title 6 * @param imageName Icon 7 * @param selectedimagename selected Icon 8 */9-(void) ADDONECHILDVC: (Uiviewcontroller *) CHILDVC title: (NSString * Title ImageName: (NSString *) imageName selectedimagename: (NSString *) SelectedImageName10 {11//Random Set sub-controller background color chil  Dvc.view.backgroundcolor=yyrandomcolor;13 14//Set title for Childvc.title=title; Equivalent to set the title of the latter two//childvc.navigationitem.title=title;//set the title of the navigation bar//childvc.tabbaritem.title=title;//     Set the title above Tabbar 18 19//Set icon Childvc.tabbaritem.image=[uiimage imagewithname:imagename];21//Set icon when selected 22 UIImage *selectedimage=[uiimage imagewithname:selectedimagename];23 if (iOS7) {26//declare this picture with original artwork ( Do not render) SelectedImage = [SelectedImage imagewithrenderingmode:uiimagerenderingmodealwaysoriginal];28}29 Ch IldVc.tabBarItem.selectedImage = selectedimage;30 31//Added as TaBbar Controller's sub-controller Yynavigationviewcontroller *nav=[[yynavigationviewcontroller Alloc]initwithrootviewcontroller: childvc];33 [self addchildviewcontroller:nav];35 36}

Description

In the project's YYTABBARVIEWCONTROLLER.M file, the 12th line of the method above, randomly set the background color of the sub-controller to create "home", "message", "discover", "I" four view when the call, overwriting the project in the button text theme settings. Moreover, from the point of view of performance, our program starts, show in front of the user only "home" this view, the other view to use, and then load. By annotating This line of code, there is no problem.

iOS development Project-05 Theme settings

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.