iOS-键盘弹出遮挡问题

来源:互联网 发布:余额宝 理财通 知乎 编辑:程序博客网 时间:2024/06/05 17:39

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //增加监听,当键盘出现或改变时收出消息

    [[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 *)aNotification

{

    //获取键盘的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

   int height = keyboardRect.size.height;

//    NSLog(@"%d",height);

    NSTimeInterval animationDuration =0.30f;

    [UIView setAnimationDuration:animationDuration];

   float width = self.view.frame.size.width;

   float vheight = self.view.frame.size.height;

    CGRect rect = CGRectMake(0.0f, -height,width,vheight);

   self.view.frame = rect;


}


//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

   self.view.frame = CGRectMake(0,0, 320, 568);

}

//释放第一响应者

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_textview resignFirstResponder];

}


0 0