UITableView onTouch隐藏键盘

时间:2012-03-14 10:45:30

标签: iphone uitableview uitextfield

我有UITableView,其中我放置了UIButton(作为单元格的子视图)。在表格下方,我有一个UITextField。触摸textField时,键盘会照常显示。我想要的是在触摸桌子时关掉键盘。

我考虑过的一个选项是为UITapGestureRecognizer设置UITableView。但是我抛出了这个想法,因为我在tableCell上有一个按钮然后变得没有响应。

另外,我不想在键盘上完成或返回按钮。我的意思是,我不希望键盘从键盘上消失,而是在触摸桌子时照顾它所具有的按钮。

2 个答案:

答案 0 :(得分:4)

可能你可以试试这个。

//NSnotification when keyboard is shown
- (void)keyboardWasShown:(NSNotification  *)notification
{    
    //  Get the size of the keyboard.
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone){

        if(UIInterfaceOrientationPortrait==orientation || UIInterfaceOrientationPortraitUpsideDown==orientation){
            keyboardSize=CGSizeMake(320.000000, 216.000000);
        }
     else if(UIInterfaceOrientationLandscapeLeft==orientation || UIInterfaceOrientationLandscapeRight==orientation)
        {
            keyboardSize=CGSizeMake(162.000000, 480.000000);
        }
    }
    else if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
        if(UIInterfaceOrientationPortrait==orientation || UIInterfaceOrientationPortraitUpsideDown==orientation)
            keyboardSize=CGSizeMake(768.000000, 264.000000);        

        else if(UIInterfaceOrientationLandscapeLeft==orientation || UIInterfaceOrientationLandscapeRight==orientation)
        {
            keyboardSize=CGSizeMake(352.000000,1024.000000);

        }
    }

    //  Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    scrvwLogig.contentInset = contentInsets;
    scrvwLogig.scrollIndicatorInsets = contentInsets;

    // Scroll the target text field into view.
    CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, txtpassword.frame.origin) ) {

        CGPoint scrollPoint=CGPointZero;
        //check flag for iPhone orientation
        if(flgLandScape)        
           scrollPoint = CGPointMake(0.0, txtpassword.frame.origin.y-70);
        //check flag for iPhone/iPad orientation
        else if(flgPort || flgPortiPad)
           scrollPoint = CGPointMake(0.0, txtpassword.frame.origin.y - (keyboardSize.height-50));        
        //check flag for ipad orientation
         else if(flgLandScapeiPad)        
          scrollPoint = CGPointMake(0.0, txtpassword.frame.origin.y-130);  

        [scrvwLogig setContentOffset:scrollPoint animated:YES];
    }
}


//when keyboard is hide

- (void) keyboardWillHide:(NSNotification *)notification {

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrvwLogig.contentInset = contentInsets;
    scrvwLogig.scrollIndicatorInsets = contentInsets;
}

答案 1 :(得分:0)

您也可以使用UIView(或UIButton)。在键盘出现之前,添加透明UIView(320x480)并在该视图的触摸事件中,您可以隐藏键盘并删除视图。