有時候這些屬性搞得蠻亂的,記錄下以便查閱
UINavigationBar屬性
-
如果想統(tǒng)一設(shè)置棚赔,可以通過以下方法帝簇,獲取當前類下的所有對象的導(dǎo)航條,然后在
+initialize
方法中進行設(shè)置
[UINavigationBar appearanceWhenContainedIn:self, nil];
- 背景圖片
- ![](http://upload-images.jianshu.io/upload_images/608238-72847773e7f3af76.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
```objc
// barMetrics需要設(shè)置成UIBarMetricsDefault
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
-
背景陰影圖片
@property(nonatomic,retain) UIImage *shadowImage
-
背景顏色
@property(nonatomic,retain) UIColor *barTintColor
-
標題文字屬性
@property(nonatomic,copy) NSDictionary *titleTextAttributes;
-
系統(tǒng)類型按鈕文字顏色
@property(nonatomic,retain) UIColor *tintColor
-
返回按鈕圖片
// 必須要兩個都設(shè)置靠益,并且圖片要設(shè)置成不渲染 @property(nonatomic,retain) UIImage *backIndicatorImage;
@property(nonatomic,retain) UIImage *backIndicatorTransitionMaskImage;
- 標題垂直偏移
- ![](http://upload-images.jianshu.io/upload_images/608238-7761bd834709f62d.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)
```objc
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics
返回按鈕更改
系統(tǒng)原裝效果:
如果有以下需求:
- 去除上面返回按鈕上“我是標題”字樣丧肴,并設(shè)置返回圖片為白色
分析
- 圖片修改
- 方式1:設(shè)置返回圖片顏色
- 方式2:直接設(shè)置返回圖片
- 方式3:使用按鈕覆蓋返回圖片(這種方式會使返回箭頭圖片和左邊距離加大,但可以用取巧的方式調(diào)整)
- 文字修改
- 方式1:設(shè)置控制器navigationItem的backBarButtonItem顯示文字為""
- 方式2:設(shè)置返回按鈕文字偏移量胧后,使其移出屏幕
- 方式3:采用控制器navigationItem的leftBarButtonItem進行覆蓋
解決
綜合以上說明芋浮,這里給出三種方式(都是針對的自定義UINavigationController):
- 方式1:在
-pushViewController:animated:
中設(shè)置文字 ,在+initialize
方法中設(shè)置返回圖片或改變返回圖片顏色- 注意導(dǎo)航欄對圖片的渲染
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
[super pushViewController:viewController animated:animated];
}
// 獲取特定類的所有導(dǎo)航條
UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
// 方式1:使用自己的圖片替換原來的返回圖片
navigationBar.backIndicatorImage = [UIImage imageNamed:@"NavBack"];
navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"NavBack"];
// 方式2:設(shè)置返回圖片顏色
navigationBar.tintColor = [UIColor whiteColor];
- 方式2:在
+initialize
方法中設(shè)置所有返回按鈕文字的偏移量壳快,其他設(shè)置和方式1一致
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
- 方式3.重寫
-pushViewController:animated:
方法,使用控制器的navigationItem的leftBarButtonItem
覆蓋返回按鈕- 需要判斷是否為根控制器纸巷,如果是根控制器就不添加
- 導(dǎo)航控制器的
viewControllers.count
不為0即表示傳入的為非根控制器
- 導(dǎo)航控制器的
- 需要判斷是否為根控制器纸巷,如果是根控制器就不添加
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count != 0) {
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
}
[super pushViewController:viewController animated:animated];
}
- (void)back
{
[self popViewControllerAnimated:YES];
}
方案對比
- 方案1和方案2改動較小江醇,對系統(tǒng)自帶的返回功能無影響。
- 方式3靈活性最高何暇,但是會
使系統(tǒng)的滑動返回失效
,需要自己實現(xiàn)凛驮,具體實現(xiàn)參照forkingdog全屏手勢分類
- 方式3還會使按鈕更加偏向右邊:
通過以下方式可以使按鈕向左邊靠:
- 采用customView裆站,
添加自己定義的UIButton
...
// 返回按鈕內(nèi)容左靠
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 讓返回按鈕內(nèi)容繼續(xù)向左邊偏移10
button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
...
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];