Styling the cancel button in a UISearchBar

前端 未结 21 1369
-上瘾入骨i
-上瘾入骨i 2020-12-02 09:13

I have a UISearchBar that has a cancel button (it\'s displayed using -(void)setShowsCancelButton:animated). I\'ve changed the tintColor of the sear

相关标签:
21条回答
  • 2020-12-02 10:04

    Well, here is function, which can change Cancel's button label. Modify it, if you want. Usage is:

    nStaticReplaceStringInView(mySearchBar, @"Cancel", @"NewCancelButtonLabel");
    
    void nStaticReplaceStringInView(UIView * view, NSString * haystack, NSString * needle)
    {
     for(int i=0; i<[view.subviews count]; i++)
     {
      nStaticReplaceStringInView([view.subviews objectAtIndex:i], haystack,needle);
     }
     if([view respondsToSelector:@selector(titleForState:)])
     {
      //NSLog(@"%@ || %@",[view titleForState:UIControlStateNormal], haystack);
      if(NSStrEq([view titleForState:UIControlStateNormal] , haystack))
      {
       [view setTitle: needle forState: UIControlStateNormal];
      }
     }
    }
    
    0 讨论(0)
  • 2020-12-02 10:06

    For iOS 11 and Swift 4. Create a subclass of UISearchController. Override method:

    override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            print("layout")
            if let btn = searchBar.subviews[0].subviews[2] as? UIButton {
                btn.frame = CGRect(x: 306, y: 20, width: 53, height: 30)
            }
    }
    
    0 讨论(0)
  • 2020-12-02 10:08

    You can use UIAppearance to style the cancel button without iterating subviews of the UISearchBar, but the UIButton header does not currently have any methods annotated with UI_APPEARANCE_SELECTOR.

    EDIT: Drill down the subviews till you get that cancel button

    But this usually returns nil until searchBar.setShowsCancelButton(true, animated: true) is called.

    extension UISearchBar {
    
    var cancelButton : UIButton? {
        if let view = self.subviews.first {
            for subView in view.subviews {
                if let cancelButton = subView as? UIButton {
                    return cancelButton
                }
            }
        }
        return nil
    }
    }
    
    0 讨论(0)
  • 2020-12-02 10:08

    You can find the cancel button by looping through the subviews of the search bar and checking for the class type (instead of the size):

    UIButton *cancelButton = nil;
    for(UIView *subView in yourSearchBar.subviews){
        if([subView isKindOfClass:UIButton.class]){
        cancelButton = (UIButton*)subView;
        }
    }
    

    And then change the tint color:

    [cancelButton setTintColor:[UIColor colorWithRed:145.0/255.0 green:159.0/255.0 blue:179.0/255.0 alpha:1.0]];
    
    0 讨论(0)
  • 2020-12-02 10:11

    For iOS 10 & above, use following method

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];
    
    0 讨论(0)
  • 2020-12-02 10:13

    stupid way

    for(id cc in [SearchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            ......
            Do whatever you want
            .......        
        }
    }
    
    0 讨论(0)
提交回复
热议问题