IOS系列——自定义导航条

导航条的自定义需要继承 UINavigationController

新建一个类:MyNav  继承 UINavigationController

判断系统版本

#define iOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

实现导航条自定义图片:在viewDidload里面添加

// 1.取出设置主题的对象
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    // 2.设置导航栏的背景图片
    NSString *navBarBg = nil;
    if (iOS7) { // iOS7
        navBarBg = @"navBG";
        navBar.tintColor = [UIColor whiteColor];        //IOS7版本才有,设置导航条的左按钮的颜色
    } else { // 非iOS7
        navBarBg = @"navBG";
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
    }
    [navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];
    
    // 3.设置标题颜色
    [navBar setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }];


在页面里面  

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];
    [self setTitle:@"第二个页面"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backBarButtonItem)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮  
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];//为导航栏左侧添加系统自定义按钮
self.title = @"页面名字";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];		//下一页出来的导航条返回按钮是  返回 两个字



UISwitch *simpleSwitch = [[UISwitch alloc] init];//实例化一个选择开关
    simpleSwitch.on = YES;//开关设置为开启状态
    [simpleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//添加事件
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];//将开关控件赋给导航栏右按钮
对开关添加事件
-(void)switchChanged:(UISwitch *)paramSender{
    if ([paramSender isOn]) {//如果开关状态为开启
        NSLog(@"Switch is on.");
    }else{
        NSLog(@"Switch is off.");
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值