美文网首页
微博(二)自定义导航栏UINavigationControlle

微博(二)自定义导航栏UINavigationControlle

作者: 破夕_____________ | 来源:发表于2016-01-12 15:47 被阅读361次

自定义导航栏,方便push(到一个新的页面的时候隐藏底栏)

  • 创建一个继承自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{
/**
 *  第一次使用这个类的时候会调用(只会调用一次)
 */
 //    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];
    }

相关文章

网友评论

      本文标题:微博(二)自定义导航栏UINavigationControlle

      本文链接:https://www.haomeiwen.com/subject/wdhckttx.html