一、設置導航欄背景全透明
-(void)viewWillAppear:(BOOL)animated{
? ? //設置導航欄背景圖片為一個空的image,這樣就透明了
? ? [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
? ? //去掉透明后導航欄下邊的黑邊
? ? [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)viewWillDisappear:(BOOL)animated{
? ? //? ? 如果不想讓其他頁面的導航欄變?yōu)橥该?需要重置
? ? [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
? ? [self.navigationController.navigationBar setShadowImage:nil];
}
二、iOS中設置導航欄標題的字體顏色和大小
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:19],
NSForegroundColorAttributeName:[UIColor redColor]}];
三、不同條件界面的跳轉(zhuǎn)
1、導航控制器的push和pop
導航控制器的跳轉(zhuǎn)方式,首先我們在appDelegate中設置其中一個viewController為導航控制器的根視圖控制器,再把這個導航控制器設置為window的根視圖控制器
AppDelegate.m//
//生成一個viewController對象
ViewController *rootVC = [[ViewController alloc]init];
//把這個對象設置為導航控制器的根視圖
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//將這個導航控制器設置為整個工程的window的根視圖控制器
self.window.rootViewController = naVC;
然后我們就可以在這個viewController里跳轉(zhuǎn)到其他界面了:[self.navigationController pushViewController:secondVC animated:YES];
然后要返回的話在那個界面安排一個回調(diào)方法,代碼可以寫:[self.navigationController popViewControllerAnimated:YES];這樣就直接返回跳轉(zhuǎn)之前的界面了
2、模態(tài)
模態(tài)比較簡單,從這個界面跳到下個界面時我們直接用以下代碼,直接讓它present出來就可以了。
[self presentViewController:showVC animated:YES completion:nil];
從新顯示的界面回來的時候用以下代碼,把它自己dismiss掉就可以了
[self dismissViewControllerAnimated:YES completion:nil];
如果既想用模態(tài)的方式又想讓第二界面的導航欄存在,那么在模態(tài)時把它添加進導航控制器就行了。
//實例化一個第二界面對象
SecondViewController *SecondVC= [[SecondViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController: SecondVC];
[self.navigationController presentViewController:naVC animated:YES completion:nil];
3、Storyboard的segues方式跳轉(zhuǎn)
此方法僅適用于Storyboard中各個頁面連線后的跳轉(zhuǎn),鼠標點擊viewControlller,按住control鍵拖拽到另一個View頁面,在彈出的segue頁面中選擇跳轉(zhuǎn)模式即可,連線完之后選中連線,在Identifier填上對應的標示,然后再在需要跳轉(zhuǎn)的地方實現(xiàn)如下代碼即可:
[self performSegueWithIdentifier:@"test" sender:self];
四、讓UIView中的Button點擊之后跳轉(zhuǎn)到另一個ViewController上去
如果使用導航
第一個按鈕方法:
[self.navigationController pushViewController:secondVC animated:YES];
第二個按鈕方法:
[self.navigationController popViewControllerAnimated:YES];
如果使用模態(tài)
第一個按鈕方法:
[self presentViewController:secondVC animated:YES completion:nil];
第二個按鈕方法:
[self dismissViewControllerAnimated:YES completion:nil];
返回
[self.navigationController popViewControllerAnimated:YES];