How to customize appearance of UISearchBar

后端 未结 9 1841
盖世英雄少女心
盖世英雄少女心 2020-12-04 09:53

I want to customize the search bar, I mean the white box. Can I do it? Is there some documentation about that? Is there a way to hide the white box, but not the letters. Can

9条回答
  •  醉酒成梦
    2020-12-04 10:28

    There're few attempts out there including subclass UISearchBar and hack it's layoutSubviews or drawLayer:. After iOS 5, the best method is to use UIAppearance.

    // Configure your images
    UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
    UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    
    // Set it to your UISearchBar appearance
    [[UISearchBar appearance] setBackgroundImage:backgroundImage];
    [[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
    

提交回复
热议问题