iOS 纯代码定制 UINavigationController导航栏 左按钮右按钮以及中间的标题
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1、创建窗口
self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
self.window.backgroundColor = [UIColorwhiteColor];
//2.创建一个导航控制器并添加子视图
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:[ViewControllernew]];
// nav.navigationBar.barTintColor = [UIColor redColor];
[nav.navigationBarsetBackgroundImage:[UIImageimageNamed:@"1"]forBarMetrics:UIBarMetricsDefault];
//3.设置根视图
self.window.rootViewController = nav;
//4、显示窗口
[self.windowmakeKeyAndVisible];
return YES;
}//定制标题
//方式1:
// self.title = @"通讯录";
//方式2:
UILabel *titleLab = [[UILabelalloc]initWithFrame:CGRectMake(0,20, 80, 44)];
titleLab.text =@"通讯录";
titleLab.textColor = [UIColorwhiteColor];
titleLab.textAlignment =NSTextAlignmentCenter;
self.navigationItem.titleView = titleLab;
//定制右按钮
//方式1:用系统自带的
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"下一页"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(Next:)];
//方式2:自己定制
UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];
but.frame =CGRectMake(0,0, 60, 44);
[but setTitle:@"下一页"forState:UIControlStateNormal];
[but addTarget:selfaction:@selector(Next:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];
self.navigationItem.rightBarButtonItem = barBut;
//定制左按钮
UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];
but.frame =CGRectMake(0,0, 60, 44);
[but setTitle:@"<返回"forState:UIControlStateNormal];
[but addTarget:selfaction:@selector(popView:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];
self.navigationItem.leftBarButtonItem = barBut;