导航条的自定义需要继承 UINavigationController
新建一个类:MyNav 继承 UINavigationController
判断系统版本
#define iOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
实现导航条自定义图片:在viewDidload里面添加
// 1.取出设置主题的对象
UINavigationBar *navBar = [UINavigationBar appearance];
// 2.设置导航栏的背景图片
NSString *navBarBg = nil;
if (iOS7) { // iOS7
navBarBg = @"navBG";
navBar.tintColor = [UIColor whiteColor]; //IOS7版本才有,设置导航条的左按钮的颜色
} else { // 非iOS7
navBarBg = @"navBG";
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
}
[navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];
// 3.设置标题颜色
[navBar setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }];
在页面里面
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];
[self setTitle:@"第二个页面"];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backBarButtonItem)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];//为导航栏左侧添加系统自定义按钮
self.title = @"页面名字";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil]; //下一页出来的导航条返回按钮是 返回 两个字
UISwitch *simpleSwitch = [[UISwitch alloc] init];//实例化一个选择开关
simpleSwitch.on = YES;//开关设置为开启状态
[simpleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//添加事件
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];//将开关控件赋给导航栏右按钮
对开关添加事件
-(void)switchChanged:(UISwitch *)paramSender{
if ([paramSender isOn]) {//如果开关状态为开启
NSLog(@"Switch is on.");
}else{
NSLog(@"Switch is off.");
}
}