iOS经典讲解之在navigationBar上面添加多个任意控件
2015-10-27 20:41
483 查看
在navigationBar上面添加多个任意控件
今天这道菜主要是在navigationBar上面加入任意数量的任何控件。
(转载请保留此文字:本文来源:[[iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件 http://blog.csdn.net/ipromiseu/archive/2010/12/16/6080474.aspx] write by Gray.Luo guohui.great@gmail.com)
基本的navigationBar上面就左,中,右 3个位置,而且默认也是添加UIBarButtonItem/UINavigationBar按钮,但是很多开发过程中会遇到在上面添加更多其它控件,经过研究后,所以特写此文,算是做个笔记,也希望能够帮助朋友解决正在解决的这方面的问题。
1.在固定位置添加UIBarButtonItem
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 上面添加多个按钮的效果
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
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
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
UIProgressView *myProgress;
myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)];
[self.navigationController.navigationBar addSubview: myProgress];
[myProgress release];
今天这道菜主要是在navigationBar上面加入任意数量的任何控件。
(转载请保留此文字:本文来源:[[iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件 http://blog.csdn.net/ipromiseu/archive/2010/12/16/6080474.aspx] write by Gray.Luo guohui.great@gmail.com)
基本的navigationBar上面就左,中,右 3个位置,而且默认也是添加UIBarButtonItem/UINavigationBar按钮,但是很多开发过程中会遇到在上面添加更多其它控件,经过研究后,所以特写此文,算是做个笔记,也希望能够帮助朋友解决正在解决的这方面的问题。
1.在固定位置添加UIBarButtonItem
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 上面添加多个按钮的效果
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
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
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
UIProgressView *myProgress;
myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)];
[self.navigationController.navigationBar addSubview: myProgress];
[myProgress release];
相关文章推荐
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android控件之CheckBox、RadioButton用法实例分析
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- WinForm实现移除控件某个事件的方法
- C#分屏控件用法实例
- 可以浮动某个物体的jquery控件用法实例
- jQuery操作表单常用控件方法小结
- C#的winform控件命名规范
- Jquery给基本控件的取值、赋值示例
- ASP.net 动态加载控件时一些问题的总结
- ASP.NET 4中的可扩展输出缓存(可以缓存页面/控件等)