ios 导航栏怎么添加左右按钮

2025-03-31 20:52:06
推荐回答(4个)
回答1:

导航栏按钮的控件叫BarButtonItem。

关于其设置:

第一种:

UIImage *searchimage=[UIImage imageNamed:@"search.png"];
UIBarButtonItem *barbtn=[[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)];
barbtn.image=searchimage;
self.navigationItem.rightBarButtonItem=barbtn;
这种设置出来的外观不好控制

第二种:

UIButton*rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,30,30)];
[rightButtonsetImage:[UIImageimageNamed:@"search.png"]forState:UIControlStateNormal];
[rightButtonaddTarget:selfaction:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]initWithCustomView:rightButton];
[rightButton release];
self.navigationItem.rightBarButtonItem= rightItem;
[rightItem release];
这种图片将填满button,大小可控

第三种:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];

如何让navigationItem.rightBarButtonItem隐藏消失?
self.navigationItem.rightBarButtonItem=nil;

即可实现

参考资料:http://blog.csdn.net/zhuzhihai1988/article/details/7701998

回答2:

  • UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];


  • UIBarButtonItem *chooseBtn = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:nil];


  • self.navigationItem.leftBarButtonItem = searchBtn;


  • self.navigationItem.rightBarButtonItem = chooseBtn;


  • 先创建UIBarButtonItem,然后再添加到导航栏,导航栏有左右item,item可以自定义,也可以用系统样式;


  • UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];


  • UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

总之要转化为UIBarButtonItem。才能加到导航条里;

如果设置图片必须用最后面的自定义类型:

把btn的背景图或直接setImage都可以

回答3:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStyleDone target:self action:@selector(myclickRightButton)]; //创建一个右边按钮

self.navigationItem.rightBarButtonItem = rightButton;
直接上代码,左边类似,导航栏这个东西建议自定义写,不建议使用系统自带,如果你的项目不是特别复杂也可以。

回答4:

左右按钮一般用来实现回到上一页,和去下一页的功能。这一般由pop,push方法完成,这个方法在UINavigationContoller中,重名字可以知道,这是个视图控制器,简而言之是个容器。管理页面间的跳转。它上面有toolbar,和navigationBar。
先看下navigationbar的构成,navigationBar中包含了这几个重要组成部分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title。
下面是代码
UIButton* leftBtn= [UIButton buttonWithType:UIButtonTypeCustom];

[leftBtn setImage:[UIImage imageNamed:@"设置按钮"] forState:UIControlStateNormal];

leftBtn.frame = CGRectMake(0, 0, 30, 30);

UIBarButtonItem* leftBtnItem = [[UIBarButtonItem alloc]initWithCustomView:pCenter];

[leftBtn addTarget:self action:@selector(setviewinfo) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setleftBarButtonItem:leftBtnItem];、

我做的是右边按钮,想做左边修改[self.navigationItem setleftBarButtonItem:leftBtnItem]为[self.navigationItem setRightBarButtonItem:leftBtnItem]
再点击事件里可以做一些push,pop等跳转页面操作。
button在最上面,item在button下,navigationbar在item下,是有层次关系的。