how to change UISearchBar textfield background color and text color in ios8

前端 未结 2 508
忘了有多久
忘了有多久 2021-02-05 23:14

I used below code for changing UISearchBar textfield background color.

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 23:40

    Just try this code

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor grayColor]];
    

    OR

    Try this

    - (void)viewDidLoad
        {
            [super viewDidLoad];
    
            //change the background color
    
     [[self searchViewForTextFieldBg:self.searchTextfield] setBackgroundColor:[UIColor grayColor]];
    
    //change the textcolor
    self.searchTextfield.textColor =[UIColor greenColor];
    
        }
    
        - (UITextField*)searchViewForTextFieldBg:(UIView*)view
    {
        if ([view isKindOfClass:[UITextField class]]) {
            return (UITextField*)view;
        }
        UITextField *searchTextField;
        for (UIView *subview in view.subviews) {
            searchTextField = [self searchViewForTextFieldBg:subview];
            if (searchTextField) {
                break;
            }
        }
        return searchTextField;
    }
    

提交回复
热议问题