自定义UINavigationController 标题、左右边按钮 navigationbar 设置多个按钮

#import <UIKit/UIKit.h>

@interface ILNavigationController : UINavigationController

@end
@implementation ILNavigationController

#pragma mark 一个类只会调用一次
+ (void)initialize
{
    // 1.取出设置主题的对象
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    // 2.设置导航栏的背景图片
    NSString *navigBG = nil;
    if (VersionNumber_iOS_7) {
        navigBG = @"DMnavigationBg7.png";

    }else{
        
        navigBG = @"DMnavigationBg6.png";
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;

        // 1.4.设置导航栏的文字
        [navBar setTitleTextAttributes:@{
                                                           UITextAttributeTextColor : kGetColor(26, 26, 26),
                                                           UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero],
                                                           UITextAttributeFont : [UIFont systemFontOfSize:18]
                                                           }];
    }
    [navBar setBackgroundImage:[UIImage imageNamed:navigBG] forBarMetrics:UIBarMetricsDefault];
    // 3.设置导航栏标题颜色为白色
    [navBar setTitleTextAttributes:@{
                                     UITextAttributeTextColor : [UIColor blackColor]
                                     }];
}

#pragma mark 控制状态栏的样式
/*
 状态栏的管理:
 1> iOS7之前:UIApplication
 2> iOS7开始:交给对应的控制器去管理
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    // 白色样式
    return UIStatusBarStyleDefault;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

@end

//调用自定义的navigation 导航栏
DMJFQListViewController *wall = [[DMJFQListViewController alloc] init];
    ILNavigationController *navigation = [[ILNavigationController alloc] initWithRootViewController:wall];
    [self presentViewController:navigation animated:YES completion:Nil];
    

设置导航栏
 // 导航栏背景
    [[UINavigationBar appearance] setTintColor:kNavigationBarColor];
    
    // 导航栏标题
    [[UINavigationBar appearance] setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}];
    
    // 状态栏颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;


//中间标题
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navLabel.text = @"团购详情";
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont systemFontOfSize:18];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;

//右边收藏按钮
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 20, 20);
[rightButton setBackgroundImage:LOAD_IMAGE(@"meishoucang") forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(doShouCang) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem;

//左边返回按钮
UIButton *fanHuiButton = [UIButton buttonWithType:UIButtonTypeCustom];
fanHuiButton.frame = CGRectMake(0, 0, 30, 40);
[fanHuiButton setBackgroundImage:LOAD_IMAGE(@"fanhuijiantou") forState:UIControlStateNormal];
[fanHuiButton addTarget:self action:@selector(doFanHui) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:fanHuiButton];
self.navigationItem.leftBarButtonItem = leftItem;<pre name="code" class="objc">


navigationBar 上设置多个按钮

 <span style="white-space:pre">	</span>UIButton *rightButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton1.frame = CGRectMake(0, 0, 38, 31);
        [rightButton1 setBackgroundImage:[UIImage imageNamed:@"pl_add_boyou.png"] forState:UIControlStateNormal];
        [rightButton1 addTarget:self action:@selector(showAddCompanion) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithCustomView:rightButton1];
        
        UIButton *rightButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton2.frame = CGRectMake(0, 0, 41, 35);
        [rightButton2 setBackgroundImage:[UIImage imageNamed:@"pl_ibo_setter.png"] forState:UIControlStateNormal];
        [rightButton2 addTarget:self action:@selector(showAddCompanion) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]initWithCustomView:rightButton2];
        
        NSArray *buttonArray = [[NSArray alloc]
                                initWithObjects:saveButton,deleteButton, nil];
        self.navigationItem.rightBarButtonItems = buttonArray;


                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值