首页 > 代码库 > 两种自定义系统弹出键盘上方的view
两种自定义系统弹出键盘上方的view
我们在很多的应用中,都可能会遇到,在弹出的键盘上方的view,添加一些控件来作辅助功能,下面我通过2种情况来介绍:
// 屏幕的物理高度 #define ScreenHeight [UIScreen mainScreen].bounds.size.height // 屏幕的物理宽度 #define ScreenWidth [UIScreen mainScreen].bounds.size.width @interface HMTMainViewController () @property (nonatomic, strong) UIView *testView; @property (nonatomic, strong) UITextField *testTextField; @property (nonatomic, strong) NSNumber *duration; @property (nonatomic, strong) NSNumber *curve; @end @implementation HMTMainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"Demo"; [self testCustonKeyBoardView___1]; [self testCustonKeyBoardView___2]; } // 自定义系统弹出键盘上方的view ---> 普遍情况 - (void)testCustonKeyBoardView___1 { UITextField *testTextField = [[UITextField alloc] initWithFrame:CGRectMake(60, 200, 200, 40)]; testTextField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:testTextField]; // 自定义的view UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; customView.backgroundColor = [UIColor redColor]; testTextField.inputAccessoryView = customView; // 往自定义view中添加各种UI控件(以UIButton为例) UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 5, 40, 30)]; button.backgroundColor = [UIColor blackColor]; [button setTitle:@"完成" forState:UIControlStateNormal]; [button addTarget:self action:@selector(didClickButtonAction) forControlEvents:UIControlEventTouchUpInside]; [customView addSubview:button]; } - (void)didClickButtonAction { NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,@"test"); [self.view endEditing:YES]; } // 自定义系统弹出键盘上方的view ---> 微信,QQ聊天等效果 - (void)testCustonKeyBoardView___2 { self.testView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 40, 320, 40)]; _testView.backgroundColor = [UIColor grayColor]; [self.view addSubview:_testView]; self.testTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 2, 200, 36)]; _testTextField.borderStyle = UITextBorderStyleRoundedRect; [_testView addSubview:_testTextField]; UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem]; leftBtn.frame = CGRectMake(242, 2, 36, 36); [leftBtn setTitle:@"-" forState:UIControlStateNormal]; leftBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0]; [_testView addSubview:leftBtn]; UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem]; rightBtn.frame = CGRectMake(282, 2, 36, 36); rightBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0]; [rightBtn setTitle:@"+" forState:UIControlStateNormal]; [_testView addSubview:rightBtn]; // 通知-监听键盘弹出事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; // 通知-监听键盘回收事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil]; } - (void)changeKeyboardWillShowNotification:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; // 键盘弹出后的frame的结构体对象 NSValue *valueEndFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // 得到键盘弹出后的键盘视图所在y坐标 CGFloat keyBoardEndY = valueEndFrame.CGRectValue.origin.y; // 键盘弹出的动画时间 self.duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; // 键盘弹出的动画曲线 self.curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; // 添加移动动画,使视图跟随键盘移动(动画时间和曲线都保持一致) [UIView animateWithDuration:[_duration doubleValue] animations:^{ [UIView setAnimationBeginsFromCurrentState:YES]; // 不设置这个,大家可以看看有什么区别 [UIView setAnimationCurve:[_curve intValue]]; _testView.center = CGPointMake(_testView.center.x, keyBoardEndY - _testView.bounds.size.height/2.0); }]; /** * + (void)setAnimationCurve:(UIViewAnimationCurve)curve; * typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { * UIViewAnimationCurveEaseInOut, // 淡入淡出,开始时慢,由慢变快,中间最快,然后变慢 * UIViewAnimationCurveEaseIn, // 淡入,开始时慢然后越来越快 * UIViewAnimationCurveEaseOut, // 淡出,开始快然后越来越慢 * UIViewAnimationCurveLinear // 线性匀速,开始和结束是一个速度 * }; */ } - (void)changeKeyboardWillHideNotification:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; // 键盘弹出前的frame的结构体对象 NSValue *valueStatrFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // 得到键盘弹出后的键盘视图所在y坐标 CGFloat keyBoardStatrY = valueStatrFrame.CGRectValue.origin.y; [UIView animateWithDuration:[_duration doubleValue] animations:^{ [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationCurve:[_curve intValue]]; _testView.center = CGPointMake(_testView.center.x, keyBoardStatrY - _testView.bounds.size.height/2.0); }]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; }
@示例图:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。