代码搭建记事本框架(二)

主题设定,初始化的代码,通过执行这些代码来设置对应的navigation和barbutton的属性

/**
 *  只执行一次的代码
 */
+(void)initialize
{
    //设置nav对应的属性.
    [self setNavigationBarTheme];
    //设置barbutton对应的属性.
    [self setBarButtonTheme ];
}

对应属性的设定

+(void)setBarButtonTheme
{
    //
    UIBarButtonItem *appearence = [UIBarButtonItem appearance];
    
    NSMutableDictionary *textAttrs =[[NSMutableDictionary alloc]init];
    textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [appearence setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *highTextAttrs =[[NSMutableDictionary alloc]init];
    highTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
    highTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [appearence setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
    
    NSMutableDictionary *disableTextAttrs =[[NSMutableDictionary alloc]init];
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [appearence setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
    
    //为了让按钮的背景消失,可以设置一张完全透明的背景图片
    [appearence setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
+(void)setNavigationBarTheme
{
    UINavigationBar *appearence  = [UINavigationBar appearance];
    
    NSMutableDictionary *textAttrs= [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [appearence setTitleTextAttributes:textAttrs];
}

定义为类方法,容易调用。

最重要的方法还是重载的push方法

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count>0) {   //如果push进来的不是栈底控制器的话
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" highImage:@"navigationbar_back_highlighted" target:self action:@selector(back)];
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" highImage:@"navigationbar_more_highlighted" target:self action:@selector(more)];
    }
    [super pushViewController:viewController animated:YES];
}

push中的来设定对应的属性,包括对应的背景和高亮背景点击时间对应的响应函数。

设定对应的按钮点击的响应内容。

-(void)more
{
    [self popToRootViewControllerAnimated:YES];
}
-(void)back
{
    [self popViewControllerAnimated:YES];
}
纸上得来终觉浅,绝知此事要躬行
原文地址:https://www.cnblogs.com/asheng/p/4399684.html