1. 程式人生 > >IOS 修改UISearchBar 輸入框的顏色 placeholder字型的顏色

IOS 修改UISearchBar 輸入框的顏色 placeholder字型的顏色


UISearchBar *searchBar = [UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

for (UIView* subview in [[searchBar.subviews lastObject] subviews]) {

        if ([subview isKindOfClass:[UITextField class]]) {

            UITextField *textField = (UITextField*)subview;

             textField.textColor = [UIColor redColor];                         //修改輸入字型的顏色

            [textField setBackgroundColor:[UIColor grayColor]];      //修改輸入框的顏色
            [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];   //修改placeholder的顏色
        } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subview removeFromSuperview];
        }
    }