本文主要是介绍自定义UINavigationController 标题、左右边按钮 navigationbar 设置多个按钮,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#import <UIKit/UIKit.h>@interface ILNavigationController : UINavigationController@end
@implementation ILNavigationController#pragma mark 一个类只会调用一次
+ (void)initialize
{// 1.取出设置主题的对象UINavigationBar *navBar = [UINavigationBar appearance];// 2.设置导航栏的背景图片NSString *navigBG = nil;if (VersionNumber_iOS_7) {navigBG = @"DMnavigationBg7.png";}else{navigBG = @"DMnavigationBg6.png";[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;// 1.4.设置导航栏的文字[navBar setTitleTextAttributes:@{UITextAttributeTextColor : kGetColor(26, 26, 26),UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero],UITextAttributeFont : [UIFont systemFontOfSize:18]}];}[navBar setBackgroundImage:[UIImage imageNamed:navigBG] forBarMetrics:UIBarMetricsDefault];// 3.设置导航栏标题颜色为白色[navBar setTitleTextAttributes:@{UITextAttributeTextColor : [UIColor blackColor]}];
}#pragma mark 控制状态栏的样式
/*状态栏的管理:1> iOS7之前:UIApplication2> iOS7开始:交给对应的控制器去管理*/
- (UIStatusBarStyle)preferredStatusBarStyle
{// 白色样式return UIStatusBarStyleDefault;
}- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{if (self.viewControllers.count) {viewController.hidesBottomBarWhenPushed = YES;}[super pushViewController:viewController animated:animated];
}@end//调用自定义的navigation 导航栏
DMJFQListViewController *wall = [[DMJFQListViewController alloc] init];ILNavigationController *navigation = [[ILNavigationController alloc] initWithRootViewController:wall];[self presentViewController:navigation animated:YES completion:Nil];设置导航栏// 导航栏背景[[UINavigationBar appearance] setTintColor:kNavigationBarColor];// 导航栏标题[[UINavigationBar appearance] setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}];// 状态栏颜色[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;//中间标题
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navLabel.text = @"团购详情";
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont systemFontOfSize:18];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;//右边收藏按钮
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 20, 20);
[rightButton setBackgroundImage:LOAD_IMAGE(@"meishoucang") forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(doShouCang) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem;//左边返回按钮
UIButton *fanHuiButton = [UIButton buttonWithType:UIButtonTypeCustom];
fanHuiButton.frame = CGRectMake(0, 0, 30, 40);
[fanHuiButton setBackgroundImage:LOAD_IMAGE(@"fanhuijiantou") forState:UIControlStateNormal];
[fanHuiButton addTarget:self action:@selector(doFanHui) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:fanHuiButton];
self.navigationItem.leftBarButtonItem = leftItem;<pre name="code" class="objc">
navigationBar 上设置多个按钮
<span style="white-space:pre"> </span>UIButton *rightButton1 = [UIButton buttonWithType:UIButtonTypeCustom];rightButton1.frame = CGRectMake(0, 0, 38, 31);[rightButton1 setBackgroundImage:[UIImage imageNamed:@"pl_add_boyou.png"] forState:UIControlStateNormal];[rightButton1 addTarget:self action:@selector(showAddCompanion) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithCustomView:rightButton1];UIButton *rightButton2 = [UIButton buttonWithType:UIButtonTypeCustom];rightButton2.frame = CGRectMake(0, 0, 41, 35);[rightButton2 setBackgroundImage:[UIImage imageNamed:@"pl_ibo_setter.png"] forState:UIControlStateNormal];[rightButton2 addTarget:self action:@selector(showAddCompanion) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]initWithCustomView:rightButton2];NSArray *buttonArray = [[NSArray alloc]initWithObjects:saveButton,deleteButton, nil];self.navigationItem.rightBarButtonItems = buttonArray;
这篇关于自定义UINavigationController 标题、左右边按钮 navigationbar 设置多个按钮的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!