UIToolBar 用法

2016-09-06  本文已影响899人  jadn

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

self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque ;

//设置工具栏的背景颜色

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

//设置添加按钮的数量

UIBarButtonItem *flexibleSpaceItem;

flexibleSpaceItem =[[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:self

action:NULL]autorelease];

[buttons addObject:flexibleSpaceItem];

//UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。

//使按钮与按钮之间有间距

UIBarButtonItem *Item;

Item = [[UIBarButtonItem alloc]

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

style:UIBarButtonItemStylePlain

target:self

action:@selector(decrement:)];

[buttons addObject:Item];

//添加第一个图标按钮

Item = [[UIBarButtonItem alloc]

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

style:UIBarButtonItemStylePlain

target:self

action:@selector(increment:)];

[buttons addObject:Item];

//添加第二个图标按钮

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 上添加 滑动条UIProgressView

UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:

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

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

[buttons addObject: myButton];       //添加滑动条

4.在UIToolBar上添加UISegmentedControl UISegmentedControl的作用是可以让有相同功能的按钮紧凑的排列在一起

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

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

segmentedControl.momentary = YES;

//如果它被设置为YES,选定时亮 点击过后不亮  如果注释掉选中的项一直被显示高亮

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

segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;//若出界的话则自动扩展其宽度

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]];

上一篇下一篇

猜你喜欢

热点阅读