这个问题很常见,相信大家也遇到过,网上的解决办法也一大堆。下面说说我遇到的问题,在此记录一下。
1.如果是普通的页面,是指在controller上直接铺控件UITextField(比如登录、注册页等),要求不是很高的可以选择在键盘弹出时,view整体上移(不管键盘是否遮挡),代码如下:
RegisterViewController.m
@interface RegisterViewController ()<UITextViewDelegate>
{
CGFloat keyboardHeight;//记录键盘的高度
}
@property (nonatomic) CGRect viewFrame; //记录当前view的frame
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.viewFrame = self.view.frame;//注意这一句,要先记录一下页面frame并赋值
[self addNotification];//注册键盘弹出、消失的通知
}
-(void)addNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//键盘弹出的时候
-(void)keyboardWillShow:(NSNotification *)notificaiton
{
CGRect keyboardFrame = [[notificaiton.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardHeight = keyboardFrame.size.height;
CGPoint point = self.view.center;
point.y -= 120;//120是根据你实际的需求,不一定是多少
[UIView animateWithDuration:0.3f animations:^{
self.view.center = point;<