Removing the image on the left of an UISearchbar

前端 未结 17 1602
攒了一身酷
攒了一身酷 2020-12-29 20:44

Can I remove the image on the left of an UISearchbar and add a new image?

相关标签:
17条回答
  • 2020-12-29 21:12

    Use this code to hide icon of search bar :-

    [searchBarItems setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    

    Take transparent image with searchIcon.jpg name and your icon hide its work for me

    0 讨论(0)
  • 2020-12-29 21:23

    Swift 4 Solution

    searchBar.setImage(UIImage(), for: .search, state: .normal)
    
    0 讨论(0)
  • 2020-12-29 21:23

    Swift 4 solution:

    searchBar.setImage(UIImage(), for: .search, state: .normal)
    

    You probably also want to adjust the gap on the left side:

    searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)
    
    0 讨论(0)
  • 2020-12-29 21:25

    If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a UITextField instead.

    0 讨论(0)
  • 2020-12-29 21:25

    You cannot easily remove the image but you can easily insert a UIImageView to replace the image, like so:

    UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[MBUIFactory imageNamed:@"ic_search_normal.png"]];
    searchIcon.frame = CGRectMake(10, 10, 24, 24);
    [self.searchBar addSubview:searchIcon];
    [searchIcon release];
    

    Remember - life is about cheating;)...

    The image has to be either pixel-perfectly aligned (play with the pixels), or the new image must have white background where it's needed to hide the original image but that does not interfere with the rest of the search bar (if you just use white background for whole image, the left corners will interfere with the background).

    0 讨论(0)
提交回复
热议问题