iphone UISearchBar Done button always enabled

2019-01-22 03:30发布

I have a UIViewController with a UISearchBar. I have replaced the Search Button by a Done button.

However, when one taps on the searchbar, the Done button is initially disabled. This occurs until one enters any character.

What I want to do is to have this Done button always enabled, such that if i tap on it i can inmediately dismiss the keyboard.

Any help? it would be highly appreciated.

I have on my UIViewController

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar  
{   
    return YES;  
}  

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}  

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText  
{  
    if (searchBar.text.length == 0)  
    {  
        //[self fixOrientation];  
        [searchBar resignFirstResponder];  
    }   
    else  
    {  
        NSLog(@"typed");  
    }  
}  


-(void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar  
{  
    NSLog(@"began");  // this executes as soon as i tap on the searchbar, so I'm guessing this is the place to put whatever solution is available  
}  

10条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-22 03:42

A simplest way and tricky is just put a blank when beginning editing search

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    //Add a blank character to hack search button enable
    searchBar.text = @" ";}
查看更多
Fickle 薄情
3楼-- · 2019-01-22 03:44

The accepted answer doesn't seem to work anymore, so I made my own category that does seem to work:

@implementation UISearchBar (enabler)

- (void) alwaysEnableSearch {
    // loop around subviews of UISearchBar
    NSMutableSet *viewsToCheck = [NSMutableSet setWithArray:[self subviews]];
    while ([viewsToCheck count] > 0) {
        UIView *searchBarSubview = [viewsToCheck anyObject];
        [viewsToCheck addObjectsFromArray:searchBarSubview.subviews];
        [viewsToCheck removeObject:searchBarSubview];
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
            @try {
                // always force return key to be enabled
                [(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
            }
            @catch (NSException * e) {
                // ignore exception
            }
        }
    }
}
查看更多
虎瘦雄心在
4楼-- · 2019-01-22 03:48

Extension for Swift:

extension UISearchBar {
    var textField: UITextField? {
        return getTextField(inViews: subviews)
    }

    private func getTextField(inViews views: [UIView]?) -> UITextField? {
        guard let views = views else { return nil }

        for view in views {
            if let textField = (view as? UITextField) ?? getTextField(inViews: view.subviews) {
                return textField
            }
        }

        return nil
    }
}

Usage:

searchBar.textField?.returnKeyType = .Done
searchBar.textField?.enablesReturnKeyAutomatically = false
查看更多
姐就是有狂的资本
5楼-- · 2019-01-22 03:50

You can get around this by looping around the subviews in the UISearchBar until you find the text field. Its then just a matter of setting "enablesReturnKeyAutomatically" to NO. Incidentally the following code also is useful for setting the keyboard type.

  // loop around subviews of UISearchBar
  for (UIView *searchBarSubview in [searchBar subviews]) {    
    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {    
      @try {
        // set style of keyboard
        [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];

        // always force return key to be enabled
        [(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
      }
      @catch (NSException * e) {        
        // ignore exception
      }
    }
  }
查看更多
孤傲高冷的网名
6楼-- · 2019-01-22 03:51

Nowadays UISearchBar conforms to UITextInputTraits. You can simply set:

searchBar.enablesReturnKeyAutomatically = NO;

WARNING: While this compiles for iOS 7.0, it will crash at runtime. It only works for >=7.1.

The docs are not clear on this one, as only since 7.1, the UISearchBar implements the UITextInputTraits protocol, but it is not noted since which iOS version the protocol is adopted.

查看更多
Root(大扎)
7楼-- · 2019-01-22 03:51

Based on the Maps app, for example, it seems the pattern should be to have a cancel button near the textfield to get rid of the keyboard. With that said, one suggestion might be to have a custom UIButton placed right in that corner that looks just like the Done button.

Kind of a hacky solution...

Hope this helps!

查看更多
登录 后发表回答