IT박스

iPhone에서 빈 영역을 터치하면 키보드를 숨기는 방법

itboxs 2020. 10. 28. 07:55
반응형

iPhone에서 빈 영역을 터치하면 키보드를 숨기는 방법


일반적으로 텍스트 입력 영역을 터치하면 키보드가 팝업되고 화면의 빈 영역을 터치하면 키보드가 사라집니다. 그렇게하는 방법?

우리가 아이폰 사파리에서 경험 한 것처럼 ...

감사합니다


업데이트 된 방법 (권장) :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   [self.view endEditing:YES];
}

그러면 모든 하위보기에 대한 편집이 종료되고 첫 번째 응답자가 사임됩니다.

다른 방법 (모든 텍스트보기에 대해 열거) :

이에 대한 단계는 다음과 같습니다.

-(IBAction) backgroundTouch : (id) sender와 같은 IBAction을보기 컨트롤러에 추가합니다.

backgroundTouch 작업에서 뷰의 모든 텍스트 상자에 resignFirstResponder 메시지를 보내야합니다. 이것은 불행하지만 현재 FirstResponder 상태 인 객체를 검색 할 방법이 없기 때문에 필요합니다. 다음과 같이 보일 것입니다.

- (IBAction)backgroundTouch:(id)sender {
  [someTextBox resignFirstResponder];
  [anotherTextBox resignFirstResponder];
}

보기에 단추 컨트롤을 추가하고 전체 표시 영역을 포함하도록 크기를 조정합니다 (상태 표시 줄, 탭 또는 탐색 컨트롤러 제외). 버튼을 선택한 다음 레이아웃 메뉴로 이동하여 맨 뒤로 보내기를 선택합니다. 또한 버튼의 유형을 사용자 정의로 설정하십시오. 이는 특별히 그리기 코드를 제공하지 않으면 표시되지 않습니다.

Button의 Touch Up Inside 이벤트를 backgroundTouch : 액션에 연결하고 시도해보십시오.


이 간단한 솔루션 사용

Swift 4의 경우 :

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
   }

Swift 2의 경우 :

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

목표 C의 경우 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   [self.view endEditing:YES];
}  

UITextField, UITextView 및 모든 하위보기에서 작동합니다.


배경에 버튼을 넣는 것을 잊어 버리십시오. 간단한 솔루션

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.textfieldName resignFirstResponder];
}

여기에 그 문제에 대한 튜토리얼이 있습니다 (DONE 키가없는 숫자 키보드에 관한 것이기도합니다) .


> iOS 4.0의 경우 또 다른 솔루션이 있습니다.

// single tap to resign keyboard
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithBlock:^(UIGestureRecognizer *rec){
            [input_text resignFirstResponder];
        }];
self.singleTapRecognizer.numberOfTapsRequired = 1;
self.singleTapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:singleTapRecognizer];

resignFirstResponder컨트롤로 보냅니다 .


다음 코드는 xamarin iOS 프로젝트에서 잘 작동합니다.

public override void TouchesBegan (NSSet touches, UIEvent evt){
    View.EndEditing (true);
}

이 방법으로도 할 수 있습니다.

보기에서 배경을 터치하여 키보드 숨기기

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self view] endEditing:YES];
}

I was having this problem too, it was very annoying. But I figured out how to get rid of the keyboard on accident. Say you are texting John, and there's a keyboard in the way from you viewing your conversations and therefore reducing the screen you can see. Well then click on messages in the uppwer left, then select a different person you have a record of texting with, then hit messages again in the upper left. Then go back and select John. No you're back at the conversation with John, by the keyboard is gone. Seams like a lot but it's pretty quick. You'd think they'd just put a hide button to on the keyboard lol Hope that helps, or was clear enough.


I could not compile Val's answer above using the 'initWithBlock:' selector.

This code works for me. Add this to the method defining the view - note that my view is composed and the subview that I want to respond to the single tap is 'chatView'. The input field view that uses the keyboard is named 'chatInput'.

    // single tap to resign (hide) the keyboard
    UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    singleTapRecognizer.numberOfTouchesRequired = 1;
    singleTapRecognizer.cancelsTouchesInView = NO;
    [chatContent addGestureRecognizer:singleTapRecognizer];

Then add the tap-handling method.

/**
 * Handles a recognized single tap gesture.
 */
- (void) handleTapFrom: (UITapGestureRecognizer *) recognizer {
    // hide the keyboard
    NSLog(@"hiding the keyboard");
    [chatInput resignFirstResponder]; 
}

This solution should work fine if you are working with swift and iOS 8/9:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
         self.view.endEditing(true)
    }

참고URL : https://stackoverflow.com/questions/804563/how-to-hide-the-keyboard-when-empty-area-is-touched-on-iphone

반응형