UISearchBar Cancel Button

前端 未结 6 2784
别那么骄傲
别那么骄傲 2021-02-20 03:35

Using a UISearchBar with showCancelButton=YES on iOS 5. Would like the cancel button to stay enabled when the keyboard drops down. Using the following code seems not to work:<

6条回答
  •  梦谈多话
    2021-02-20 04:26

    Just to improve upon what Kurt Spindler said on his post. Though this might not be superior but it is more contained. I use dispatch to do the same thing.

    -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
        for (UIView *subview in searchBar.subviews)
        {
            if ([subview isKindOfClass:[UIButton class]])
            {
                int64_t delayInSeconds = .001;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    UIButton * cancelButton = (UIButton *)subview;
                    [cancelButton setEnabled:YES];
                });
                break;
            }
        }
    }
    

    This should work for everyone who needs help keep cancel enabled. Make sure that you hide it later either with the cancel or Search clicked.

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
        [searchBar resignFirstResponder];
        [searchBar setShowsCancelButton:NO animated:YES];
    }
    

提交回复
热议问题