Dismissing keyboard from UISearchBar when the X button is tapped

前端 未结 14 1273
眼角桃花
眼角桃花 2020-12-24 07:42

I\'m using the UISearchBar (but not the SearchDisplayController that\'s typically used in conjunction) and I\'d like to dismiss the keyboard when you hit the \'X\' button.

14条回答
  •  青春惊慌失措
    2020-12-24 08:23

    EDIT: Actually, the below breaks the delegate that is connected to UISearchBar. Just subclass UISearchBar and overwrite the UITextField Delegate method.

    ===========================

    I had to do this to get access to the UITextView

    for (UIButton* v in [self.searchBar.subviews[0] subviews])
    {
        if ( [v isKindOfClass: [UITextField class]] )
        {
            UITextField *tf = (UITextField *)v;
            tf.delegate = self;
            break;
        }
    }
    

提交回复
热议问题