APP开发之UINavigationController设置

前言:
搭建APP的时候少不了设置UITabBarController,UINavigationController;
UITabBarController是为了管理控制器,而UINavigationController是为了统一布局;

本篇介绍导航控制器,主要涉及到以下“控件”的设置

一、UINavigationBar

1、导航控制器的整个背景颜色

self.navigationBar.barTintColor = [UIColor redColor];//设置为红色

2、设置文字颜色和大小

    //    设置UINavigationBar文字属性
    UINavigationBar *appearance = [UINavigationBar appearance];

    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];

    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [appearance setTitleTextAttributes:textAttrs];

二、UIBarButtonItem

这里分一般和自定义进行设置:

1、一般情况:(与设置UINavigationBar一样)
UIBarButtonItem *appearance = [UIBarButtonItem appearance];


    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
2、自定义:重写导航控制器的 pushViewController 方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ 

    if (self.viewControllers.count > 0) {
        /** 推出控制器之后隐藏tabBar */
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithBg:@"icon_return" title:@"返回" target:self action:@selector(back)];
        **这里对BarButtonItem进行自定义**
    }  
        [super pushViewController:viewController animated:YES];  
}

-(void)back
{   
        [self popViewControllerAnimated:YES];   
}
对BarButtonItem进行扩展

+ (UIBarButtonItem *)barButtonItemWithBg:(NSString *)bg title:(NSString *)title target:(id)target action:(SEL)action

{
    UIButton *button = [[UIButton alloc]init];

    [button setImage:[UIImage imageNamed:bg] forState:UIControlStateNormal];

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];

    [button setTitle:title forState:UIControlStateNormal];

    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -55, 0, 0)];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];

    button.frame = CGRectMake(0, 0, 60, 44);

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值