self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"RightBarButton" style:UIBarButtonItemStylePlain target:self action:@selector(tapRightBarButtonItem)]
见代码UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]
btn.frame = CGRectMake(0, 0, 44, 44)
btn.adjustsImageWhenHighlighted = NO
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]
[btn addTarget:self action:Action forControlEvents:UIControlEventTouchUpInside]
UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn]
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
negativeSpacer.width = -12//ios7以后右边距默认值18px,负数相当于右移,正数左移
self.navigationItem.leftBarButtonItems=@[negativeSpacer,item]
左右按钮一般用来实现回到上一页,和去下一页的功能。这一般由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下,是有层次关系的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)