分享

iOS自定义键盘弹出view的位置移动(三种方法)

 南山岳麓书院 2015-07-02

当弹出键盘时,自定义键盘上方的view,有三种解决办法:一个就是利用UITextField或者UITextView的inputAccessoryView属性,另一种,就是监听键盘弹出的notification来自己解决相关视图的位置问题。还有一种是覆盖 TextFileld 的一些方法。

第一种解决方法相对比较简单,第二种的方法中有一个难题就是当键盘的输入方式,也就是中英文切换时,键盘的高度是会发生变化的。需要动态来调整相关视图的位置。

设定inputAccessoryView属性

UITextField或者UITextView有一个inputAccessoryView的属性,其类型是UIView。使用中,可以自定义一个view,并将这个view传递给inputAccessoryView的属性即可。这种实现方式相对简单,可以满足很多情况的需求了。下面给出一些示例代码。

// 新建一个UITextField,位置及背景颜色随意写的。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, 200, 20)];
textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:textField];    // 自定义的view
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
customView.backgroundColor = [UIColor lightGrayColor];
textField.inputAccessoryView = customView;    // 往自定义view中添加各种UI控件(以UIButton为例)
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 5, 60, 20)];
btn.backgroundColor = [UIColor greenColor];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btn];

上面代码很简单,一看就明白了。这里的键盘时通过UITextField的becomeFirstResponder后弹出的。而我在开发中就碰到了一种情况,就是需要通过点击一个按钮来弹出键盘,同时键盘上方的自定义视图中需要包含一个UITextView。这时,这种情况就不适用了。需要用到第二种方法。

监听键盘事件动态改变自定义view位置

这种方法的思路就是首先自己写一个view,然后监听键盘的事件,得到键盘的位置后调整自己写的view的位置,保证这个view的下边界与键盘的上边界相接。在自定义view中包含一个UITextField或者UITextView。通过代码调用其becomeFirstResponder方法来弹出键盘。

下面写一些关键代码,其中自定义的view名为_mainView,全局变量。

监听键盘事件代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(changeContentViewPosition:)  name:UIKeyboardWillHideNotification object:nil];

移除监听

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotificatition object:nil];

事件处理函数
// 根据键盘状态,调整_mainView的位置
- (void) changeContentViewPoint:(NSNotification *)notification{    
NSDictionary *userInfo = [notification userInfo];    
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    
CGFloat keyBoardEndY = value.CGRectValue.origin.y;  
// 得到键盘弹出后的键盘视图所在y坐标 
NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];  
// 添加移动动画,使视图跟随键盘移动
 [UIView animateWithDuration:duration.doubleValue animations:^{ [UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationCurve:[curve intValue]];
 _mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - _mainView.bounds.size.height/2.0);  // keyBoardEndY的坐标包括了状态栏的高度,要减去 }];}
其中添加了一个动画,使得过渡效果好一点。 mainView中即可添加自定义的UI控件。注意,这个mainView中控件要从最下面开始布局,因为上述代码是以下方为准的。
一开始,我也选择了第二种,可是当点击的时候,会发生延迟现象,还有救是中英文的问题,后来查资料,找到了第三种解决办法。

覆盖 UITextField 的方法

UITextField 的方法有如下:

@interface UIView (UITextField)
- (BOOL)endEditing:(BOOL)force;    // use to make the view or any subview that is the first responder resign (optionally force)
@end
@protocol UITextFieldDelegate <NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;   // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;    // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;   // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;   // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField;  // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;   // called when 'return' key pressed. return NO to ignore.
@end
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;

以下是我写的代码

//textField 变成第一响应着,意味着点击了 textField,然后改变textFieldView 的位置坐标
- (void)textFieldDidBeginEditing:(UITextField *)textField  {     
CGRect rect = self.textFieldView.frame;    
rect.origin.y = self.view.frame.size.height - 216 - 10;        
NSTimeInterval animationDuration = 0.3f;   
 [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
 [UIView setAnimationDuration:animationDuration]; 
self.textFieldView.frame = rect; [UIView commitAnimations];
}
//点击虚拟键盘上的 return 按钮后
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 
   [self.textField resignFirstResponder];  
   return YES;
}
//对textFieldView 的位置调整,并返回 YES,从来返回
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
 CGRect rect = self.textFieldView.frame; 
self.textFieldView.frame = CGRectMake(0, self.view.frame.size.height - 49 + 10, rect.size.width, rect.size.height);  
return YES;
}
 这样就能改变了,其中前两种方法为拷贝,尊重作者权利,转载。

http:///2013/01/09/ios-keyboard-custom-upperview/  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多