UIToolBar 用法

1. 在UIToolBar 上添加相同的控件

self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque ;

//設(shè)置工具欄的背景顏色

NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:4];

//設(shè)置添加按鈕的數(shù)量

UIBarButtonItem *flexibleSpaceItem;

flexibleSpaceItem =[[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:self

action:NULL]autorelease];

[buttons addObject:flexibleSpaceItem];

//UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系統(tǒng)提供的用于占位的按鈕樣式。

//使按鈕與按鈕之間有間距

UIBarButtonItem *Item;

Item = [[UIBarButtonItem alloc]

initWithImage:[UIImage imageNamed:@"down.png"]

style:UIBarButtonItemStylePlain

target:self

action:@selector(decrement:)];

[buttons addObject:Item];

//添加第一個(gè)圖標(biāo)按鈕

Item = [[UIBarButtonItem alloc]

initWithImage:[UIImage imageNamed:@"up.png"]

style:UIBarButtonItemStylePlain

target:self

action:@selector(increment:)];

[buttons addObject:Item];

//添加第二個(gè)圖標(biāo)按鈕

flexibleSpaceItem = [[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:self

action:NULL]autorelease];

[buttons addObject:flexibleSpaceItem];

UIToolbar *toolBar = [[UIToolbar alloc]init];

toolBar.barStyle = UIBarStyleBlackOpaque ;

[toolBar setItems:buttons animated:YES];

[toolBar sizeToFit];

self.navigationItem.titleView = toolBar ;

//將這些按鈕添加到toolbar上面去

2.在UIToolBar 上添加 UILabel

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];

myLabel.font=[UIFont systemFontOfSize:10];

myLabel.backgroundColor = [UIColor whiteColor];

myLabel.textAlignment=UITextAlignmentCenter;

myLabel.text ?= @"aa";

UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];

[buttons addObject: myButtonItem]; ? ? //添加文本

3.在UIToolBar 上添加 滑動(dòng)條UIProgressView

UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:

CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];

UIBarButtonItem *myButton = [[UIBarButtonItem alloc]initWithCustomView:myProgress];

[buttons addObject: myButton]; ? ? ? //添加滑動(dòng)條

4.在UIToolBar上添加UISegmentedControl UISegmentedControl的作用是可以讓有相同功能的按鈕緊湊的排列在一起

NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];

UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];

segmentedControl.momentary = YES;

//如果它被設(shè)置為YES,選定時(shí)亮 點(diǎn)擊過后不亮 ?如果注釋掉選中的項(xiàng)一直被顯示高亮

[(UITextView *)self.view setText:@""];

segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;//若出界的話則自動(dòng)擴(kuò)展其寬度

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

segmentedControl.frame = CGRectMake(0, 0, 400, 30);

[segmentedControl addTarget:self action:@selector(segmentAction:)

forControlEvents:UIControlEventValueChanged];

//self.navigationItem.titleView = segmentedControl;

[self.navigationController.navigationBar addSubview: segmentedControl]; //與上面一句話同樣的效果

-(void) segmentAction: (id) sender

{

switch([sender selectedSegmentIndex] + 1)

{

case 1: [(UITextView *) self.view setText:@"\n\nOne"]; break;

case 2: [(UITextView *) self.view setText:@"\n\nTwo"]; break;

case 3: [(UITextView *) self.view setText:@"\n\nThree"]; break;

case 4: [(UITextView *) self.view setText:@"\n\nFour"]; break;

default: break;

}

}

5.在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];

6.在UIToolBar上添加image

[myToolBarItems addObject:[[[UIBarButtonItem alloc]

initWithImage:[UIImage imageNamed:@"myImage.png"]

style:UIBarButtonItemStylePlain

target:self

action:@selector(action)]];

7.在UIToolBar上添加SystemItem

[myToolBarItems addObject:[[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemPlay

target:self

action:@selector(action)] autorelease]];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容