How do you keep the cancel button in the search bar enabled when the keyboard is dismissed?

前端 未结 11 1788
萌比男神i
萌比男神i 2020-12-30 01:23

\"enter \"enter

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 02:06

    Call to [self.searchBar resignFirstResponder] will make the cancel button disabled. Hence, you should always update cancel button to enable after calling it.

    Objective-C

    [searchBar resignFirstResponder];
    UIButton *cancelButton = (UIButton *)[searchBar valueForKey:@"cancelButton"];
    [cancelButton setEnabled:YES];
    

    Swift

    searchBar.resignFirstResponder()
    if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton
        cancelButton.isEnabled = true
    }
    

    In my experience, view.endEditing(true) is the problem. Because it's also called .resignFirstResponder if there's a UITextField inside the view, which is contained in UISearchBar.

    https://developer.apple.com/reference/uikit/uiview/1619630-endediting

提交回复
热议问题