微博(二)自定義導航欄UINavigationController

自定義導航欄,方便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]; }

自定義導航欄的主題,樣式,文字樣式,同樣是在UINavigationController控制器里面

代碼如下


/**

  • 加號方法 必須用加號+(void)settingThem
    */

+(void)initialize{

[self settingTheme];


[self settingBarButtonItemTheme];

}
//設置導航欄的主題

+(void)settingTheme{
/**
 *  第一次使用這個類的時候會調(diào)用(只會調(diào)用一次)
 */
 //    1.取出appearance對象
UINavigationBar *navBar = [UINavigationBar appearance];

 //   2.設置背景 ios6
//    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];

/**這ios7以后新的設置方法
 *  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;
 */
//設置顏色
textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
//    設置陰影
textAttrs[NSShadowAttributeName] = nil;
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];

[navBar setTitleTextAttributes:textAttrs];

}

/**

  • 設置導航欄按鈕主題
    */
  • (void)settingBarButtonItemTheme
    {
    UIBarButtonItem *item = [UIBarButtonItem appearance];

    // 設置背景
    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];
    }

    // 設置文字屬性
    //這是舊的設置方法
    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];
    }

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容