Change text color and border color of a UISearchBar

…衆ロ難τιáo~ 提交于 2019-12-11 09:41:15

问题


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.



来源:https://stackoverflow.com/questions/23886159/change-text-color-and-border-color-of-a-uisearchbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!