Removing the image on the left of an UISearchbar

前端 未结 17 1833
攒了一身酷
攒了一身酷 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:11

    To completely remove the icon, you can use the following lines of code:

    Objective-C:

    // Remove the icon, which is located in the left view
    [UITextField appearanceWhenContainedIn:[UISearchBar class], nil].leftView = nil;
    
    // Give some left padding between the edge of the search bar and the text the user enters
    [UISearchBar appearance].searchTextPositionAdjustment = UIOffsetMake(10, 0);
    

    Swift:

    // Remove the icon, which is located in the left view
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).leftView = nil
    
    // Give some left padding between the edge of the search bar and the text the user enters
    UISearchBar.appearance().searchTextPositionAdjustment = UIOffsetMake(10, 0)
    

提交回复
热议问题