Imitation Sina Weibo IOS client (v5.2.8) -- set the navigation bar appearance, iosv5.2.8
Reprinted please indicate the source: http://blog.csdn.net/android_ls/article/details/45849447
Statement: similar to the Sina Weibo project, all image resources are sourced from the official website.Sina WeiboFor IOS clients, the purpose of writing this application is to learn and communicate. If infringement is involved, please inform us that I will promptly Replace the images used.
1. Add in Xcode6. for those who are familiar with Xcode of the previous version, after upgrading to Xcode 6 with the system reminder, Xcode will not be created for us after the new project is created. pch file. But we are used to writing some macros that are used in many places to the. pch file. How can we add one without this file? Next, we will add a. pch File to our imitation Sina Weibo project. The specific steps are as follows: 1. Right-click Supporting Files and choose New File..., as shown in
2. Create a PCH file, for example:
3. Select Next and Create. Next, modify the configuration file, enter "Prefix Header" in the search box, and enter the relative path of the pch file created in the previous step in the Prefix Header field. After the modification, see:
2. Based on the previous article, set the navigation bar appearance. The specific implementation is as follows:
1. Create a WBNavigationController class to inherit from UINavigationController. Replace the UINavigationController class with our custom WBNavigationController. The Code is as follows:
// Use the system's default UINavigationController // [self addChildViewController: [[UINavigationController alloc] initWithRootViewController: childViewController]; // use our custom navigation bar (WBNavigationController inherits from UINavigationController) WBNavigationController * navigationController = [[WBNavigationController alloc] initWithRootViewController: childViewController]; [self addChildViewController: navigationController];
2. Add the UIBarButtonItem in the WBNavigationController. m file and set the appearance style of all UIBarButtonItem in the project. The Code is as follows:
# Pragma mark is triggered only once at run time # pragma mark is worth noting that the method of the parent class has been executed once before, so you do not need to call this function of super. + (Void) initialize {// set the UIBarButtonItem exterior style UIBarButtonItem * item = [UIBarButtonItem appearance] for all uibarbuttonitems throughout the project; // set it under UIControlStateNormal, size and color of the text in the navigation bar [item setTitleTextAttributes: @ {NSForegroundColorAttributeName: kColor (64, 64), NSFontAttributeName: [UIFont systemFontOfSize: 15]} forState: UIControlStateNormal // set the text size and color in the navigation bar under UIControlStateHighlighted [item setTitleTextAttributes: @ {custom: kColor (253,109, 10), NSFontAttributeName: [UIFont systemFontOfSize: 15]} forState: UIControlStateHighlighted];}
3. Intercept All controllers that are pushed in, and set the navigation bar appearance of controllers other than the root controller. The specific implementation is as follows:
# Pragma mark rewrite this method objective: to intercept all controllers that push in # pragma mark viewController this parameter is the Controller to be pushed in-(void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated {if (self. viewControllers. count> 0) {// if the Controller viewController that is pushed in is not the root controller // automatically display and hide the tabbar viewController. hidesBottomBarWhenPushed = YES; // set the return button viewController on the left of the navigation bar. navigationItem. leftBarButtonItem = [UIBarButtonItem itemWithTarget: self action: @ selector (back) image: @ "navigationbar_back_withtext" highImage: @ "navigationbar_back_withtext_highlighted" title: self. title];} [super pushViewController: viewController animated: animated];}-(void) back {[self popViewControllerAnimated: YES];}
4. Set the buttons in the navigation bar of the home page. The specific implementation is as follows:
/// HomeViewController. m // SinaWeibo o // Created by android_ls on 15/5/17. // Copyright (c) 2015 android_ls. all rights reserved. /// homepage dynamic list controller # import "HomeViewController. h "# import" UIBarButtonItem + Category. h "# import" FriendAttentionStatusViewController. h "@ interface HomeViewController () @ end @ implementation HomeViewController-(void) viewDidLoad {[super viewDidLoad]; // set the button self on the left of the navigation bar. navigationItem. leftBarButtonItem = [UIBarButtonItem leftBarButtonItemWithTarget: self action: @ selector (friendsearch) image: @ "navigationbar_friendsearch" highImage: @ "Custom"]; // set the pop-up menu button self on the right side. navigationItem. rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTarget: self action: @ selector (pop) image: @ "navigationbar_pop" highImage: @ "navigationbar_pop_highlighted"];} # pragma mark open friend follow dynamic controller-(void) friendsearch {MyLog (@ "the user clicked the button on the left"); FriendAttentionStatusViewController * friendAttentionStatusVC = [[FriendAttentionStatusViewController alloc] init]; [self. navigationController pushViewController: friendAttentionStatusVC animated: YES];} # pragma mark pop-up drop-down menu-(void) pop {MyLog (@ "the user clicked the drop-down menu button on the right");} @ end
5. implemented as follows:
Click the button on the left of the Home Page:
Click me In the ToolBar at the bottom, as shown below:
Click the Settings button in the navigation bar as follows:
Iii. Other files currently used
1. The uview + Category. h code is as follows:
/// UIView + Category. h // SinaWeibo o // Created by android_ls on 15/5/19. // Copyright (c) 2015 android_ls. all rights reserved. // # import <UIKit/UIKit. h> @ interface UIView (Category) @ property (nonatomic, assign) CGFloat x; @ property (nonatomic, assign) CGFloat y; @ property (nonatomic, assign) CGFloat width; @ property (nonatomic, assign) CGFloat height; @ property (nonatomic, assign) CGPoint origin; @ property (nonatomic, assign) CGSize size; @ property (nonatomic, assign) CGFloat centerX; @ property (nonatomic, assign) CGFloat centerY; @ end
The UIView + Category. m code is as follows:
/// UIView + Category. m // SinaWeibo o // Created by android_ls on 15/5/19. // Copyright (c) 2015 android_ls. all rights reserved. // # import "UIView + Category. h "@ implementation UIView (Category)-(void) setX :( CGFloat) x {CGRect frame = self. frame; frame. origin. x = x; self. frame = frame;}-(CGFloat) x {return self. frame. origin. x;}-(void) setY :( CGFloat) y {CGRect frame = self. frame; frame. origin. y = y; self. frame = frame;}-(CGFloat) y {return self. frame. origin. y;}-(void) setWidth :( CGFloat) width {CGRect frame = self. frame; frame. size. width = width; self. frame = frame;}-(CGFloat) width {return self. frame. size. width;}-(void) setHeight :( CGFloat) height {CGRect frame = self. frame; frame. size. height = height; self. frame = frame;}-(CGFloat) height {return self. frame. size. height;}-(void) setOrigin :( CGPoint) origin {CGRect frame = self. frame; frame. origin = origin; self. frame = frame;}-(CGPoint) origin {return self. frame. origin;}-(void) setSize :( CGSize) size {CGRect frame = self. frame; frame. size = size; self. frame = frame;}-(CGSize) size {return self. frame. size;}-(void) setCenterX :( CGFloat) centerX {CGPoint center = self. center; center. x = centerX; self. center = center;}-(CGFloat) centerX {return self. center. x;}-(void) setCenterY :( CGFloat) centerY {CGPoint center = self. center; center. y = centerY; self. center = center;}-(CGFloat) centerY {return self. center. y ;}@ end
2. UIBarButtonItem classification. The UIBarButtonItem + Category. h code is as follows:
/// UIBarButtonItem + Category. h // SinaWeibo o // Created by android_ls on 15/5/19. // Copyright (c) 2015 android_ls. all rights reserved. // # import <UIKit/UIKit. h> @ interface UIBarButtonItem (Category) # pragma mark sets the appearance style of the buttons consisting of the left text and images + (UIBarButtonItem *) itemWithTarget :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage title :( NSString *) title; # pragma mark sets the appearance style of the buttons on the left (only images) + (UIBarButtonItem *) leftBarButtonItemWithTarget :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage; # pragma mark sets the appearance style of the button on the right (only images) + (UIBarButtonItem *) rightBarButtonItemWithTarget :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage; @ end
The UIBarButtonItem + Category. m code is as follows:
/// UIBarButtonItem + Category. m // SinaWeibo o // Created by android_ls on 15/5/19. // Copyright (c) 2015 android_ls. all rights reserved. // # import "UIBarButtonItem + Category. h "@ implementation UIBarButtonItem (Category) # pragma mark sets the appearance style of the buttons composed of the text and images on the left + (UIBarButtonItem *) itemWithTarget :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage title :( NSString *) title {UIButt On * btn = [UIButton buttonWithType: UIButtonTypeCustom]; [btn addTarget: target action: action forControlEvents: UIControlEventTouchUpInside]; [btn identifier: bytes]; [btn. titleLabel setFont: [UIFont systemFontOfSize: 15]; [btn setTitle: title? Title: @ "return" forState: UIControlStateNormal]; [btn setTitle: title? Title: @ "Returned" forState: UIControlStateHighlighted]; [btn setTitleColor: kColor (64, 64, 64) forState: UIControlStateNormal]; [btn setTitleColor: kColor (253,109, 10) forState: break]; [btn setImage: [UIImage imageNamed: image] forState: UIControlStateNormal]; [btn setImage: [UIImage imageNamed: highImage] forState: UIControlStateHighlighted]; // set the size of btn. size = CGSizeMake (60, 44); // adjust the left margin CGFloat left =-8; btn of UIBarButtonItem. imageEdgeInsets = UIEdgeInsetsMake (0, left, 0, 0); btn. titleEdgeInsets = UIEdgeInsetsMake (0, left, 0, 0); return [[UIBarButtonItem alloc] initWithCustomView: btn] ;}# pragma mark sets the appearance style of the left button (only images) + (UIBarButtonItem *) identifier :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage {UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; [btn sessions: Events]; [btn addTarget: target action: action forControlEvents: UIControlEventTouchUpInside]; [btn setImage: [UIImage imageNamed: image] forState: UIControlStateNormal]; [btn setImage: [UIImage imageNamed: highImage] forState: UIControlStateHighlighted]; // set the size of btn. size = CGSizeMake (60, 44); // adjust the outer margin CGFloat left =-8; btn on the Right of UIBarButtonItem. imageEdgeInsets = UIEdgeInsetsMake (0, left, 0, 0); return [[UIBarButtonItem alloc] initWithCustomView: btn] ;}# pragma mark sets the appearance style of the button on the right (only images) + (UIBarButtonItem *) identifier :( id) target action :( SEL) action image :( NSString *) image highImage :( NSString *) highImage {UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; [btn sessions: Events]; [btn addTarget: target action: action forControlEvents: UIControlEventTouchUpInside]; [btn setImage: [UIImage imageNamed: image] forState: UIControlStateNormal]; [btn setImage: [UIImage imageNamed: highImage] forState: UIControlStateHighlighted]; // set the size of btn. size = CGSizeMake (60, 44); // adjust the outer margin CGFloat right =-8; btn on the right of UIBarButtonItem. imageEdgeInsets = UIEdgeInsetsMake (0, 0, 0, right); return [UIBarButtonItem alloc] initWithCustomView: btn];} @ end
3. The content of the info. pch file is as follows:
/// Info. pch // SinaWeibo o /// Created by android_ls on 15/5/19. // Copyright (c) 2015 android_ls. all rights reserved. // # ifndef SinaWeibo_info_pch # define SinaWeibo_info_pch # ifdef _ OBJC __# import <UIKit/UIKit. h> # import <Foundation/Foundation. h> # pragma mark: Extended class for importing UIView # import "UIView + Category. h "# endif // obtain the RGB color # define kColor (r, g, B) [UIColor colorWithRed :( r)/255.0 green :( g)/255.0 blue :( B) /255.0 alpha: 1] // log output macro definition # ifdef DEBUG // DEBUG status # define MyLog (...) NSLog (_ VA_ARGS _) # else // release status # define MyLog (...) # endif
4. the source code of the ProfileViewController. m file is as follows:
//// ProfileViewController. m // SinaWeibo o // Created by android_ls on 15/5/17. // Copyright (c) 2015 android_ls. all rights reserved. // # import "ProfileViewController. h "# import" SettingViewController. h "@ interface ProfileViewController () @ end @ implementation ProfileViewController-(void) viewDidLoad {[super viewDidLoad]; // set UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "set" style: 0 target: self action: @ selector (setting)]; [barButtonItem setTitleTextAttributes: @ {NSForegroundColorAttributeName: kColor (253,109, 10), NSFontAttributeName: [UIFont systemFontOfSize: 15]} forState: UIControlStateNormal]; self. navigationItem. rightBarButtonItem = barButtonItem;}-(void) setting {MyLog (@ "the user clicked the set button"); SettingViewController * settingViewController = [[SettingViewController alloc] init]; [self. navigationController pushViewController: settingViewController animated: YES];} @ end
It's not too early. We'll be here today, good night.
Source code: http://download.csdn.net/detail/android_ls/8718359