UIBarButtonItem导航栏添加按钮

1 前言
UIBarButtonItem为导航栏按钮,在导航栏的左侧和右侧,他们具有许多种不同的形状和形式。

2 代码讲解
ZYViewController.m

[plain]
 (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.title = @"First"; 
    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:)];//为导航栏左侧添加系统自定义按钮 

 
-(void)perFormAdd:(id)paramSender{ 
    NSLog(@"Action method got called."); 
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"First";
    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:)];//为导航栏左侧添加系统自定义按钮
}

-(void)perFormAdd:(id)paramSender{
    NSLog(@"Action method got called.");
}运行结果:

当点击左边和右边的按钮的时候,控制台显示:

2013-04-23 21:40:58.982 UIBarButtonItemTest[660:c07] Action method got called.

2013-04-23 21:41:02.598 UIBarButtonItemTest[660:c07] Action method got called.

ZYUIBarButtonViewController.m:

[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.title = @"Second"; 
    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."); 
    } 
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"Second";
    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.");
    }
}
运行结果:

当拨动开关控制台显示:

2013-04-23 21:46:46.692 UIBarButtonItemTest[727:c07] Switch is off.

2013-04-23 21:46:47.493 UIBarButtonItemTest[727:c07] Switch is on.

时间: 2024-08-28 03:16:42

UIBarButtonItem导航栏添加按钮的相关文章

UI: 使用 UIBarButtonItem 给导航栏添加按钮

问题: 希望将按钮添加到导航栏中 1.导航栏属于 UINavigationBar 类,你可以再任何时候创建它,并将它添加到任意的 view 中. 2.创建一个导航按钮,须要做一下工作: 创建一个 UIBarButtonItem 实例. 使用视图控制器的 navigationItem 属性将按钮添加到视图控制器的导航栏中,. NavigationItem 属性允许我们与导航栏进行交互.这个属性自身有两个属性,分别为 rightBarButtonItem 和 leftBarButtonItem.这两

iOS开发项目篇—04添加导航栏的按钮

iOS开发项目篇—04添加导航栏的按钮 一.设置导航栏的按钮 要求实现的效果:             说明:默认状态下和高亮状态下的图片是不一样的. 按钮的图片需要设置默认状态和高亮状态时的显示,系统了提供的下面方法 viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#>

iOS开发项目—04添加导航栏的按钮

iOS开发项目—04添加导航栏的按钮 一.设置导航栏的按钮 要求实现的效果:             说明:默认状态下和高亮状态下的图片是不一样的. 按钮的图片需要设置默认状态和高亮状态时的显示,系统了提供的下面方法 viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#>

iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法. 在iOS属性后有UI_APPEARANCE_SELECTOR标志都可以一次性统一设置.这种情况还有很多.比如说统一设置UITabbarItem的文字颜色 通过appearance来同意设置属性最好是在+ (void)initialize;方法里面. 项目中设置导航栏背景图片的代码: 项目中设置T

iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案

为相机制作闪光灯,在导航栏自定义了"闪光"图案,希望点击时变换图片,但是一直没有改变,原来是因为设置了Global Tint的颜色,所以系统会自动把图片的颜色改为Global Tint的颜色. 解决方案,设置图片时,添加:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal 源码: - (void) setFlashOn:(BOOL)isOn { if (self.captureDevice.hasFlash) { UIIm

小技巧 -- 隐藏导航栏返回按钮附带的文字

使用 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; 方法可以隐藏导航栏返回按钮上的文字.代码放的位置是每一个总的Controller的viewDidLoad方法下面.

自定义导航栏返回按钮文字

自定义导航栏返回按钮文字 by 伍雪颖 navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

iOS 调整导航栏返回按钮的位置

调整导航栏返回按钮的位置 //创建返回按钮 UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem]; leftBtn.frame = CGRectMake(0, 0, 25,25); [leftBtn setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [leftBtn addTarget:se

Swift - 重写导航栏返回按钮

// 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // 给按钮设置返回箭头图片 backButton.setImage(UIImage(named: "NavigationBar_goBack_icon"), for: .normal) // 设置frame backButton.frame = CGRect(x: 200, y: 13, w