删除UISearchbar左侧的图像

问题描述 投票:23回答:16

我可以删除UISearchbar左侧的图像并添加新图像吗?

iphone uisearchbar
16个回答
-4
投票

如果你的意思是放大镜,那么没有。没有公共API可以更改或删除它。只需使用UITextField


3
投票

Swift 4解决方案:

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

您可能还想调整左侧的间隙:

searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)

2
投票

您可以继承UISearchBar,然后使用此方法:

- (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;
    }
}

1
投票

使用此代码隐藏搜索栏的图标: -

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

使用searchIcon.jpg名称获取透明图像,您的图标隐藏了我的工作


0
投票

您无法轻松删除图像,但您可以轻松插入UIImageView来替换图像,如下所示:

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

记住 - 生活就是作弊;)......

图像必须是像素完美对齐(与像素一起播放),或者新图像必须具有白色背景,其中需要隐藏原始图像但不会干扰搜索栏的其余部分(如果您只是使用整个图像的白色背景,左角会干扰背景)。


0
投票

在ios6中至少有一个[searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]

属性你可以设置:)


0
投票

对于特定的UISearchBar实例。

Objective-C的:

// Get the inner search field.
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"searchField"];

// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];

-2
投票

其实你可以。您需要浏览搜索栏的所有子视图,然后搜索搜索文本字段。


39
投票

在iOS 5.0+中,您可以使用自定义搜索栏图像

- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state

在UISearchBar的特定实例中或使用UIAppearance代理的一堆实例。


11
投票
// hide magnifying glass
    UITextField* searchField = nil;
    for (UIView* subview in searchBar.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
            searchField = (UITextField*)subview;
            break;
        }
    }
    if (searchField) {
        searchField.leftViewMode = UITextFieldViewModeNever;
    }

这隐藏了放大镜。效果很好。


11
投票

要完全删除图标,可以使用以下代码行:

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);

迅速:

// 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)

10
投票

在swift 2.0中这样做:

let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never

6
投票

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

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];

5
投票

或者在Swift 4.0中简单的单行:

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

注意:这将删除UITextFields中包含的所有UISearchBars的左手图标。


5
投票

Swift 4解决方案

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

3
投票

用1x1透明图像替换搜索栏图标并偏移文本位置

UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);
© www.soinside.com 2019 - 2024. All rights reserved.