導航條的自定義需要繼承 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.");
}
}