在navigationBar上面新增多個任意控制元件
基本的navigationBar上面就左,中,右 3個位置,而且預設也是新增UIBarButtonItem/UINavigationBar按鈕,但是很多開發過程中會遇到在上面新增更多其它控制元件,經過研究後,所以特寫此文,算是做個筆記,也希望能夠幫助朋友解決正在解決的這方面的問題。
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];
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 *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];
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];
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];
UIProgressView *myProgress;
myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)];
[self.navigationController.navigationBar addSubview: myProgress];
[myProgress release];
小結:通過上面的方法 ,應該可以拋磚引玉,讓你自己新增其他任意控制元件。還等什麼呢?趕快試一下吧,讓你的navigationBar條豐富多彩吧