惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 龚祺

写在产品公测发布前 如何联机调试和发布程序 Post a UIImage to the web 关于String 由pushViewController说起可能出线的各种死法 Sizes of iPhone UI Elements NSString 常用 转载-无言 经过慎重考虑,最终决定上D60 2009项目运行初始 前进的驿站 隆冬季节的随笔 创造一家伟大的公司 卸去浮华 不再浮躁 成功需要积累 如何处理团队意见 感染所有人 思想的力量 猫仔网诞生了 为战斗而生存!
在navigationBar上面添加多个任意控件
龚祺 · 2011-11-18 · via 博客园 - 龚祺

今天这道菜主要是在navigationBar上面加入任意数量的任何控件。

    基本的navigationBar上面就左,中,右 3个位置,而且默认也是添加UIBarButtonItem/UINavigationBar按钮,但是很多开发过程中会遇到在上面添加更多其它控件,经过研究后,所以特写此文,算是做个笔记,也希望能够帮助朋友解决正在解决的这方面的问题。

1.在固定位置添加UIBarButtonItem

  1. UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]  
  2.                 initWithTitle:@"myButton"  
  3.                 style:UIBarButtonItemStyleBordered  
  4.                 target:self   
  5.                 action:@selector(action)]autorelease];  
  6. self.navigationItem.leftBarButtonItem = myButton;  
  7.   
  8.   
  9. [myButton release];  

  NavigationItem类有以下一些成员:

-title

-titleview

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

-rightBarButtonItem

-leftBarButtonItem

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

  1. UIToolbar *mycustomToolBar;  
  2. NSMutableArray *mycustomButtons = [[NSMutableArray alloc] init];  
  3. UIBarButtonItem *myButton1 = [[[UIBarButtonItem alloc]  
  4.                 initWithTitle:@"Get5"  
  5.                 style:UIBarButtonItemStyleBordered  
  6.                 target:self   
  7.                 action:@selector(action)]autorelease];  
  8. myButton1.width = 40;  
  9. [mycustomButtons addObject: myButton1];  
  10. UIBarButtonItem *myButton2 = [[[UIBarButtonItem alloc]  
  11.                 initWithTitle:@"Play5"  
  12.                 style:UIBarButtonItemStyleBordered  
  13.                 target:self   
  14.                 action:@selector(action)]autorelease];  
  15. myButton2.width = 40;  
  16. [mycustomButtons addObject: myButton2];   
  17.   
  18. mycustomToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 44.0f)];  
  19.   
  20. mycustomToolBar.barStyle = UIBarStyleDefault;  
  21. [mycustomToolBar setItems:mycustomButtons animated:YES];  
  22. [mycustomToolBar sizeToFit];      
  23. [self.view addSubview:mycustomToolBar];  
  24.   
  25.   
  26. [mycustomToolBar release];  
  27. [mycustomButtons release];  
 

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

3.在任意位置添加UISegmentedControl

  1. UISegmentedControl * mySegment;  
  2. mySegment = [[UISegmentedControl alloc]  
  3.                initWithFrame:CGRectMake(5.0f, 10.0, 60.0f, 30.0f)];  
  4. [mySegment insertSegmentWithTitle:@"mySeg1" atIndex:0 animated:YES];   
  5. [get5Segment insertSegmentWithTitle:@"mySeg2" atIndex:1 animated:YES];    
  6. mySegment.segmentedControlStyle = UISegmentedControlStyleBar;  
  7. [mySegment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];  
  8. mySegment.selectedSegmentIndex = -1;  
  9. [self.navigationController.navigationBar addSubview: mySegment];  
  10. [mySegment release];  

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

4.在任意位置添加UILabel

  1. UILabel* myLabel;  
  2. myLabel=[[UILabel alloc] initWithFrame:CGRectMake(100.0f, 14.0f, 100.0f, 10.0f)];  
  3. myLabel.font=[UIFont systemFontOfSize:10];  
  4. myLabel.backgroundColor = [UIColor clearColor];  
  5. [self.navigationController.navigationBar addSubview: myLabel];  
  6. [myLabel release];  
 

5.在任意位置添加UIProgressView 

  1.     UIProgressView *myProgress;  
  2. myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)];  
  3. [self.navigationController.navigationBar addSubview: myProgress];  
  4. [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)