Change text color and border color of a UISearchBa

2019-08-03 00:22发布

问题:

I have an UISearchBar, with search style "minimal", and I want to change the text color and to have a white border with 1px width. I'm trying with tintcolor but I don't get nothing

Thank you.

回答1:

To change the text color of the UISearchBar, use this snippet

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor yourColor]];

For the white border, try this out

for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
{
    if ([object isKindOfClass:[UITextField class]])
    {
        UITextField *textFieldObject = (UITextField *)object;

        textFieldObject.textColor = [UIColor redColor];
        textFieldObject.layer.borderColor = [[UIColor whiteColor] CGColor];
        textFieldObject.layer.borderWidth = 1.0;
        break;
    }
}

Hope this helps!

Edit

You can add the first line for the search bar color anywhere in your app. Also, I have added one line for the textColor in the loop, check that out.