簡體   English   中英

UITableView onTouch隱藏鍵盤

[英]UITableView onTouch hide keypad

我有一個UITableView其中放置了一個UIButton (作為單元格的子視圖)。 在表格下方,我還有一個UITextField 觸摸textField時,鍵盤照常顯示。 我想要的是在觸摸桌子時關閉鍵盤。

我考慮的一個選項是為UITableView設置UITapGestureRecognizer 但是我提出了這個想法,因為在tableCell上有一個按鈕,該按鈕隨后變得無響應。

另外,我也不想鍵盤上的完成或返回按鈕。 我的意思是,我不希望鍵盤從鍵盤上消失,而是在觸摸桌子時要注意它所具有的按鈕。

也許您可以嘗試一下。

//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;
}

您也可以使用UIView (或UIButton)。 在鍵盤出現之前,添加一個透明的UIView (320x480),並且在該視圖發生觸摸事件時,您可以隱藏鍵盤並刪除該視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM