UIToolbar

- (void)viewDidLoad {
  [super viewDidLoad];
  [self AddToolBars];

}

-(void)AddToolBars
{
UIToolbar *toolBarTop=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)];
[toolBarTop setBarStyle:UIBarStyleDefault];
[self.view addSubview:toolBarTop];
[toolBarTop setBarTintColor:[UIColor redColor]];

UIBarButtonItem *item1=[[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarItemCilck)];
item1.width=20;

UIBarButtonItem *item2=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarItemCilck)];
item2.width=20;

UIBarButtonItem *item4=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
item2.width=20;


UIBarButtonItem *item3=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil ];

ToolBarItem *textField=[[ToolBarItem alloc] init];//自定义的UIview
textField.frame=CGRectMake(0, 0, 200, 30);
[textField.button addTarget:self action:@selector(btnClick1) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *item5=[[UIBarButtonItem alloc]initWithCustomView:textField];//自定义的Item


[toolBarTop setItems:[NSArray arrayWithObjects:item1,item5,nil] animated:YES];

}

@interface ToolBarItem : UIView

-(id)init
{
self= [super init];
_button=[[UIButton alloc] initWithFrame:CGRectMake(70, 0, 30, 30)];
[_button setTitle:@"Click" forState:UIControlStateNormal];
_button.backgroundColor=[UIColor grayColor];

UITextField *t1=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
t1.placeholder=@"Hello";
t1.backgroundColor=[UIColor orangeColor];

[self addSubview:t1];
[self addSubview:_button];

return self;
}

原文地址:https://www.cnblogs.com/zhangleixy/p/4975889.html