UITextAttributeTextColor 的替换方法+自定义导航控制器的价值

UITextAttributeTextColor 的替换方法  

     UITextAttributeTextColor 已经在iOS7.0以后被推荐使用了,UITextAttributeTextColor = deprecated in iOS 7.0,改为推荐使用UITextAttributeTextColor类来代替,具体替换方法以及相关示例代码如下:

  - 使用UITextAttributeTextColor的方法源代码如下所示:

    // 3.设置导航栏主题
    UINavigationBar *navBar = [UINavigationBar appearance];
    // 设置背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    // 设置标题文字颜色和字体大小
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[UITextAttributeTextColor] = [UIColor whiteColor];
    attrs[UITextAttributeFont] = [UIFont systemFontOfSize:16];
    [navBar setTitleTextAttributes:attrs];

  - 使用UITextAttributeTextColor类代替上述代码如下:

1. 更新后的第一种方法:

    // 设置标题文字颜色和字体大小
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
        attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
        [navBar setTitleTextAttributes:attrs];

或者:

2. 更新后的第二种方法:

    // 3.设置导航栏主题
    UINavigationBar *navBar = [UINavigationBar appearance];
    // 设置背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    // 设置标题文字颜色和字体大小
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:16], NSFontAttributeName,nil]];

    但是在用最后一种方法实现时,自定义导航控制器以后,运行后控制器中的view都不见了。因此还是推荐使用更新后的第一种方法。

-》但是当我为了简便起见:不用每个导航控制器的子控制器都需要逐个勾选  hide Bottom on Push。

  - 即自定义导航控制器,重写show方法,并且让stroyboard里面的相应导航控制器都定义为自定义的导航控制器,这样就可以减少很多麻烦。

		- 重写push方法就可以拦截所有压入栈中的子控制器,统一做一些处理
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

		- 重写pop方法就可以拦截所有子控制器的出栈
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
/**
 *  重写这个方法,能拦截所有的push操作
 *
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];

或者也可以重写相应的pop方法;

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    return [super popViewControllerAnimated:NO];
}

敬请指正。

原文地址:https://www.cnblogs.com/wangmaster/p/5111436.html