自动创建 按钮和搜索框

- (IBAction) CreatedToolbar

{

[[searchBar.subviews objectAtIndex:0]removeFromSuperview];

//[[toolBar.subviews objectAtIndex:0]removeFromSuperview];

//toolBar.backgroundColor = [UIColor redColor];

toolBar.barStyle=UIBarStyleBlackTranslucent;

//搜索框

UISearchBar *searchBarr = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];

UIBarButtonItem  * searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarr];

//UIToolbar  * searchToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0+20,[self view].bounds.size.width,32)];

UIBarButtonItem   *freshButton=[[UIBarButtonItem allocinitWithBarButtonSystemItem: UIBarButtonSystemItemRefresh     target:self   action:@selector(showMessge)]; 

// [freshButton setImage:[UIImage imageNamed:@"ICON/1.png"] forState:UIControlStateNormal];

//button1

UIImage *image = [UIImage imageNamed:@"ICON/1.png"];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

//button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );   

button2.bounds = CGRectMake( 0, 0, 100, 44 );  

[button2 setImage:image forState:UIControlStateNormal];

[button2 addTarget:self action:@selector(showMessge) forControlEvents:UIControlEventTouchUpInside];    

[button2 setBackgroundColor:[UIColor redColor]];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button2];

//button2

UIImage *Img1= [UIImage imageNamed:@"ICON/1.png"];

UIImage *Img2 = [UIImage imageNamed:@"ICON/2.png"];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.adjustsImageWhenDisabled = YES;

[btn setEnabled:NO];

[btn setImage:Img2 forState:UIControlStateNormal];

[btn setImage:Img1 forState:UIControlStateDisabled];

btn.frame = CGRectMake(0, 0, Img1.size.width, Img1.size.height);

[btn addTarget:self action:@selector(showMessge) forControlEvents:UIControlEventTouchUpInside]; 

[btn setBackgroundColor:[UIColor redColor]];

UIBarButtonItem *shuffleBarItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

//button3

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

[button1 setBackgroundColor:[UIColor redColor]];

[button1 setBackgroundImage:[UIImage imageNamed:@"ICON/1.png"] forState:UIControlStateNormal];

[button1 setTitle:@"Delete" forState:UIControlStateNormal];

button1.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];

[button1.layer setCornerRadius:4.0f];

[button1.layer setMasksToBounds:YES];

[button1.layer setBorderWidth:1.0f];

[button1.layer setBorderColor: [[UIColor grayColor] CGColor]];

button1.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);

[button1 addTarget:self action:@selector(showMessge) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* Item1 = [[UIBarButtonItem alloc] initWithCustomView:button1];

//button4

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

backButton.frame = CGRectMake(0, 0, 10, 29.0);

[backButton setImage:[UIImage imageNamed:@"ICON/1.png"] forState:UIControlStateNormal];

[backButton setBackgroundColor:[UIColor redColor]];

UIBarButtonItem* btnn = [[UIBarButtonItem alloc] initWithCustomView:backButton];

//[freshButton release]; 

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 setItems:[NSArray arrayWithObjects:searchBarButtonItem,freshButton,btnn,Item1,barButtonItem,shuffleBarItem, nil] animated:YES];

//[myToolBar release];

//[myToolBarItems release];

[self.view addSubview:myToolBar];

// //首先需要创建一个NSMutableArray   

// NSMutableArray *buttons=[[NSMutableArray  alloc]initWithCapacity:3];  

// [buttons  autorelease];  

//

// //创建一个 UIBarButtonItem 系统刷新按钮  并且加入到Array  

// UIBarButtonItem   *freshButton=[[UIBarButtonItem alloc]  initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh     target:self   action:@selector(OnrefreshMap:)];  

// [buttons addObject:freshButton];  

// [freshButton release];  

// //创建一个空格 ,加入到array,用来将下面加入的按钮按照右边对齐  

// UIBarButtonItem   *SpaceButton=[[UIBarButtonItem alloc]  initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace     target:nil   action:nil];  

// [buttons addObject:SpaceButton];  

// [SpaceButton release];  

//

// //创建一个 系统 搜索按钮,加入到array,放到右边  

// UIBarButtonItem   *searchSelfButton=[[UIBarButtonItem alloc]  initWithBarButtonSystemItem: UIBarButtonSystemItemSearch     target:self   action:@selector(OnFindSelf:)];  

// [buttons addObject:searchSelfButton];  

// [searchSelfButton release];  

//

// //最后,将array 设置给toolbar  

// [myToolBar setItems:buttons animated:YES];  

// [myToolBar  sizeToFit];  

}

你可能感兴趣的:(创建)