Removing the image on the left of an UISearchbar

前端 未结 17 1600
攒了一身酷
攒了一身酷 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 20:59

    How to change the position or hide magnifier icon in UISearchBar in IOS 7?

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
    
    0 讨论(0)
  • 2020-12-29 20:59

    You can use

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

    I tried all answers on iOS 12 and 13 - and none worked correctly on both versions. The only correct answer is below:

    searchField.leftViewMode = .never
    if #available(iOS 13.0, *) {
        searchTextPositionAdjustment = UIOffset(horizontal: -20, vertical: 0);
    }
    
    0 讨论(0)
  • 2020-12-29 21:00

    in ios6 at least there is a [searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]

    property u can set :)

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

    Or in Swift 4.0 the simple one-liner:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
    

    NOTE: This will remove the left hand icon from ALL UITextFields that are contained inside UISearchBars.

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

    you can subclass UISearchBar and then use this method:

    - (void)setTextFieldLeftView:(UIView *)view
    {
        UITextField *searchField = nil;
        for (UIView *subview in self.subviews)
        {
            if ([subview isKindOfClass:[UITextField class]])
            {
                searchField = (UITextField *)subview;
                break;
            }
        }
    
        if (searchField != nil) 
        {
            searchField.leftView = view;
        }
    }
    
    0 讨论(0)
提交回复
热议问题