在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
 
 

  NavigationItem类有以下一些成员:

-title

-titleview

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

-rightBarButtonItem

-leftBarButtonItem

 

 

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

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

3.在任意位置添加UISegmentedControl
 
如果要在navigationBar实现多个按钮,而且某个功能块的类似按钮需要挨在一起,用segment实现还是很不错,用UIBarButtonItem实现的话,按钮间总是有一个间隔。
4.在任意位置添加UILabel
 
5.在任意位置添加UIProgressView 
 
小结:通过上面的方法 ,应该可以抛砖引玉,让你自己添加其他任意控件。还等什么呢?赶快试一下吧,让你的navigationBar条丰富多彩吧!(转载请保留此文字:本文来源:[[iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件 http://blog.csdn.net/ipromiseu/archive/2010/12/16/6080474.aspx] write by Gray.Luo guohui.great@gmail.com)
 
 
 
 
 
 

 

根据SDK的文档,我们可以发现UIBbarButtonItem有如下几种初始化的方法:

-initWithTitle

-initWithImage

-initWithBarButtonSystemItem

-initWithCustomView

1:在UIToolBar上面添加Title
UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
NSMutableArray *myToolBarItems = [NSMutableArray array];
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithTitle:@"myTile" 
    style:UIBarButtonItemStylePlain
    target:self 
    action:@selector(action)] autorelease]];
[myToolBar setItems:myToolBarItems animated:YES];
[myToolBar release];
[myToolBarItems];
2:在UIToolBar上面添加image
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]
                                                    style:UIBarButtonItemStylePlain
            target:self
    action:@selector(action)]];
3:在UIToolBar上面添加SystemItem
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
    target:self 
    action:@selector(action)] autorelease]];

SystemItem主要有:

    UIBarButtonSystemItemDone,

    UIBarButtonSystemItemCancel,

    UIBarButtonSystemItemEdit,  

    UIBarButtonSystemItemSave,  

    UIBarButtonSystemItemAdd,

    UIBarButtonSystemItemFlexibleSpace,

    UIBarButtonSystemItemFixedSpace,

    UIBarButtonSystemItemCompose,

    UIBarButtonSystemItemReply,

    UIBarButtonSystemItemAction,

    UIBarButtonSystemItemOrganize,

    UIBarButtonSystemItemBookmarks,

    UIBarButtonSystemItemSearch,

    UIBarButtonSystemItemRefresh,

    UIBarButtonSystemItemStop,

    UIBarButtonSystemItemCamera,

    UIBarButtonSystemItemTrash,

    UIBarButtonSystemItemPlay,

    UIBarButtonSystemItemPause,

    UIBarButtonSystemItemRewind,

    UIBarButtonSystemItemFastForward,

4:使用initWithCustomView

A>加一个开关switch:

[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithCustomView:[[[UISwitch alloc] init] 

autorelease]];  

 

posted @ 2015-12-28 15:23  Bo-tree  阅读(206)  评论(0)    收藏  举报