天天看點

有關UITextField、UITextView 軟鍵盤彈出遮蓋等問題的處理

。h檔案中實作UITextFieldDelegate,UITextViewDelegate委托

。m檔案:

如果隻要簡單實作編輯功能 實作UITextFieldDelegate的某些相關方法就行

#pragma mark - UITextFieldDelegate method

-(void)textFieldDidBeginEditing:(UITextField

*)textField{//先于鍵盤通知事件執行

    editRect =

textField.frame;

}

-(void)textFieldDidEndEditing:(UITextField *)textField{

 NSString *dd = @"dfdfd";

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField

resignFirstResponder];

    return

YES;

在- (void)viewDidLoad中注冊鍵盤通知事件:在ios4.0和5.0中都有用 通過測試模拟器的

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyBoardWillShow:)

name:UIKeyboardWillShowNotification object:nil];//鍵盤将要顯示的通知

selector:@selector(keyBoardWillHidden:)

name:UIKeyboardWillHideNotification object:nil];//鍵盤将要隐藏的通知

-(void)keyBoardWillShow:(NSNotification *)notif{

    NSDictionary

*info = [notif userInfo];

    NSValue

*value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

    CGSize

keyboardSize = [value CGRectValue].size;

//整個UIViewControll中加一個scrollView控件 然後再把各種控件放在其中就行

NSTimeInterval animation = 0.50f;

    [UIView

beginAnimations:@"animal" context:nil];

setAnimationDuration:animation];

    CGRect

scrollViewFrame = [mainScroll frame];

scrollViewFrame.size.height -=

(keyboardSize.height-48);//減去tabBar的高度

content = [contentView frame];

content.size.height += (keyboardSize.height-48);

contentView.frame = content;

mainScroll.frame = scrollViewFrame;

    [mainScroll

scrollRectToVisible:editRect animated:YES];

commitAnimations];

-(void)keyBoardWillHidden:(NSNotification *)notif{

scrollViewFrame.size.height += (keyboardSize.height-48);

content.size.height -= (keyboardSize.height-48);

網絡上志同道合,我們一起學習網絡安全,一起進步,QQ群:694839022