ios navigationbar 不能添加控件是怎么回事

ios navigationbar 不能添加控件是怎么回事,第1张

1.在固定位置添加UIBarButtonItem

view plaincopy to clipboardprint?

UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]

initWithTitle:@"myButton"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(action)]autorelease]

self.navigationItem.leftBarButtonItem = myButton

//self.navigationItem.rightBarButtonItem = myButton

//self.navigationItem.backBarButtonItem = myButton

[myButton release]

NavigationItem类有以下一些成员:

-title

-titleview

-backBarButtonItem//这是有返回上一级事件的后退按钮

-rightBarButtonItem

-leftBarButtonItem

2.在任意位置添加一个UIToolbar叠加到navigationBar上,然后设置其背景透明,则可以实现在上这个navigationBar 上面添加多个按钮的效果

view plaincopy to clipboardprint?

UIToolbar *mycustomToolBar

NSMutableArray *mycustomButtons = [[NSMutableArray alloc] init]

UIBarButtonItem *myButton1 = [[[UIBarButtonItem alloc]

initWithTitle:@"Get5"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(action)]autorelease]

myButton1.width = 40

[mycustomButtons addObject: myButton1]

UIBarButtonItem *myButton2 = [[[UIBarButtonItem alloc]

initWithTitle:@"Play5"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(action)]autorelease]

myButton2.width = 40

[mycustomButtons addObject: myButton2]

mycustomToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 44.0f)]

//mycustomToolBar.center = CGPointMake(160.0f,200.0f)

mycustomToolBar.barStyle = UIBarStyleDefault

[mycustomToolBar setItems:mycustomButtons animated:YES]

[mycustomToolBar sizeToFit]

[self.view addSubview:mycustomToolBar]

//self.navigationItem.titleView = mycustomToolBar//与上一句都可实现在上面叠加工具条

//将toolbar的颜色设置为透明,总之使用两个控件叠加完美

[mycustomToolBar release]

[mycustomButtons release]

这里是在UIToolbar 上面添加UIBarButtonItem,然而我们很多时候可能会添加其它控件,如:switch,label等等,所以在UIToolbar上面如何添加各种控件,就参考下一篇文章。

3.在任意位置添加UISegmentedControl

view plaincopy to clipboardprint?

UISegmentedControl * mySegment

mySegment = [[UISegmentedControl alloc]

initWithFrame:CGRectMake(5.0f, 10.0, 60.0f, 30.0f)]

[mySegment insertSegmentWithTitle:@"mySeg1" atIndex:0 animated:YES]

[get5Segment insertSegmentWithTitle:@"mySeg2" atIndex:1 animated:YES]

mySegment.segmentedControlStyle = UISegmentedControlStyleBar

[mySegment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged]

mySegment.selectedSegmentIndex = -1

[self.navigationController.navigationBar addSubview: mySegment]

[mySegment release]

如果要在navigationBar实现多个按钮,而且某个功能块的类似按钮需要挨在一起,用segment实现还是很不错,用UIBarButtonItem实现的话,按钮间总是有一个间隔。

4.在任意位置添加UILabel

view plaincopy to clipboardprint?

UILabel* myLabel

myLabel=[[UILabel alloc] initWithFrame:CGRectMake(100.0f, 14.0f, 100.0f, 10.0f)]

myLabel.font=[UIFont systemFontOfSize:10]

myLabel.backgroundColor = [UIColor clearColor]

[self.navigationController.navigationBar addSubview: myLabel]

[myLabel release]

5.在任意位置添加UIProgressView

view plaincopy to clipboardprint?

UIProgressView *myProgress

myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)]

[self.navigationController.navigationBar addSubview: myProgress]

[myProgress release]

(转载)

在uinavigationbar中间加button方法如下:

1、如果修改后退按钮的标题,必须在先前的view controller里修改,而不是在将要显示的地方修改,如果曾在这个上面放置另一个view controller,那就把后退按钮称为back,而不是default(默认)”。

2、在某个特殊状态下隐藏后退按钮,比如显示UIPickerView时,使用self.navigationItem.hidesBackButton = YES当退出这一状态时,记得设置回原样。

3、显示某个特殊的象征性按钮,使用带有UIBarButtonSystemItemAdd这类控件的

具体代码如下:

导航栏按钮的控件叫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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11294758.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存