How to hide the keyboard when touching screen (search bar)

前端 未结 4 1504
执笔经年
执笔经年 2020-12-30 11:45

The keyboard hides when I click search or when I click on cancel. But I want also that the keyboard hides when I click somewhere on the screen. I found several tutorials for

4条回答
  •  时光取名叫无心
    2020-12-30 12:05

    Try This

    in your .h file add UISearchBar

    @property (strong, nonatomic) IBOutlet UISearchBar *searchBar; 
    

    in your .m file

    - (void)viewDidLoad
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];
    
        [self.view addGestureRecognizer:tap];
    }
    
    
    - (void) dismissKeyboard
    {
        // add self
        [self.searchBar resignFirstResponder];
    }
    

提交回复
热议问题