实现效果如下:
代码实现如下:
// // ViewController.m // PopToolBarDemo // // Created by 思 彭 on 2017/7/20. // Copyright © 2017年 思 彭. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *myTextField; @property (nonatomic, strong) UIToolbar *toolBarView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.myTextField.delegate = self; // 监听键盘的弹出 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; CGSize size = self.view.frame.size; // 创建一个工具条,并设置它的大小和位置 UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, size.width, 46)]; self.toolBarView = toolbar; // 设置工具条的style [toolbar setBarStyle:UIBarStyleDefault]; // 创建使用文本标题的UIBarButtonItem // UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)]; UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)]; [spaceItem setTag:1]; // 创建使用自定义图片的UIBarButtonItem UIBarButtonItem* centerItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"exam_stopwatch_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)]; [centerItem setTag:2]; // 创建使用系统图标的UIBarButtonItem UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClick:)]; [rightItem setTag:3]; // 为工具条设置工具按钮 注意设置间距 NSArray* barButtonItems = [NSArray arrayWithObjects:spaceItem, centerItem, spaceItem, rightItem,spaceItem, nil]; [toolbar setItems:barButtonItems animated:YES]; // 将工具条添加到当前应用的界面中 [self.view addSubview:toolbar]; } - (void)onClick: (UIBarButtonItem *)item { NSInteger tag = [item tag]; switch (tag) { case 2: NSLog(@"第一个"); break; case 3: NSLog(@"第二个"); break; default: break; } } - (void)textFieldDidEndEditing:(UITextField *)textField { NSLog(@"textField.text = %@",textField.text); } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.myTextField resignFirstResponder]; return YES; } - (void)keyBoardWillChangeFrame:(NSNotification*)notification{ // 键盘显示隐藏完毕的frame CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 动画时间 CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; if (frame.origin.y == [UIScreen mainScreen].bounds.size.height) { // 没有弹出键盘 [UIView animateWithDuration:duration animations:^{ self.toolBarView.transform = CGAffineTransformIdentity; }]; } else{ // 弹出键盘 // 工具条往上移动258 [UIView animateWithDuration:duration animations:^{ self.toolBarView.transform = CGAffineTransformMakeTranslation(0, -frame.size.height-44); }]; } } @end