自定義導(dǎo)航欄,方便push(到一個新的頁面的時候隱藏底欄)
- 創(chuàng)建一個繼承自UINavigationController的控制器
- 重寫-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 這個方法
具體代碼如下
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.viewControllers.count > 0) { viewController.hidesBottomBarWhenPushed = YES; } [super pushViewController:viewController animated:animated]; }
自定義導(dǎo)航欄的主題,樣式,文字樣式,同樣是在UINavigationController控制器里面
代碼如下
/**
- 加號方法 必須用加號+(void)settingThem
*/
+(void)initialize{
[self settingTheme];
[self settingBarButtonItemTheme];
}
//設(shè)置導(dǎo)航欄的主題
+(void)settingTheme{
/**
* 第一次使用這個類的時候會調(diào)用(只會調(diào)用一次)
*/
// 1.取出appearance對象
UINavigationBar *navBar = [UINavigationBar appearance];
// 2.設(shè)置背景 ios6
// [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
// [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
/**這ios7以后新的設(shè)置方法
* UIKIT_EXTERN NSString *const UITextAttributeFont NS_DEPRECATED_IOS(5_0, 7_0, "Use NSFontAttributeName") __TVOS_PROHIBITED;
// Key to the text color in the text attributes dictionary. A UIColor instance is expected.
UIKIT_EXTERN NSString *const UITextAttributeTextColor NS_DEPRECATED_IOS(5_0, 7_0, "Use NSForegroundColorAttributeName") __TVOS_PROHIBITED;
// Key to the text shadow color in the text attributes dictionary. A UIColor instance is expected.
UIKIT_EXTERN NSString *const UITextAttributeTextShadowColor NS_DEPRECATED_IOS(5_0, 7_0, "Use NSShadowAttributeName with an NSShadow instance as the value") __TVOS_PROHIBITED;
// Key to the offset used for the text shadow in the text attributes dictionary. An NSValue instance wrapping a UIOffset struct is expected.
UIKIT_EXTERN NSString *const UITextAttributeTextShadowOffset NS_DEPRECATED_IOS(5_0, 7_0, "Use NSShadowAttributeName with an NSShadow instance as the value") __TVOS_PROHIBITED;
*/
//設(shè)置顏色
textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
// 設(shè)置陰影
textAttrs[NSShadowAttributeName] = nil;
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
[navBar setTitleTextAttributes:textAttrs];
}
/**
- 設(shè)置導(dǎo)航欄按鈕主題
*/
-
(void)settingBarButtonItemTheme
{
UIBarButtonItem *item = [UIBarButtonItem appearance];// 設(shè)置背景
if (!iOS7) {
[item setBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[item setBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background_pushed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[item setBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background_disable"] forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
}// 設(shè)置文字屬性
//這是舊的設(shè)置方法
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[UITextAttributeTextColor] = iOS7 ? [UIColor orangeColor] : [UIColor grayColor];
textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero];
textAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:iOS7 ? 14 : 12];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
}