删除 UISearchbar 左侧的图像

发布于 2024-09-09 21:39:33 字数 51 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

鹿童谣 2024-09-16 21:39:33

自定义搜索栏图像。

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

在 iOS 5.0+ 中,您可以在 UISearchBar 的特定实例中或使用 UIAppearance 代理在一组中

In iOS 5.0+, you can customize the search bar image using

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

in a specific instance of a UISearchBar or across a bunch using the UIAppearance proxy.

不醒的梦 2024-09-16 21:39:33

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

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)

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)
凶凌 2024-09-16 21:39:33
// 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;
    }

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

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

This hides magnifying glass. Works well.

一个人的旅程 2024-09-16 21:39:33

在 swift 2.0 中这样做:

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

In swift 2.0 do this:

let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
山色无中 2024-09-16 21:39:33

Swift 4 解决方案

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

Swift 4 Solution

searchBar.setImage(UIImage(), for: .search, state: .normal)
隔岸观火 2024-09-16 21:39:33

Swift 4解决方案:

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

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

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

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)
追风人 2024-09-16 21:39:33

或者在 Swift 4.0 中,简单的一句:

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

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

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.

扛起拖把扫天下 2024-09-16 21:39:33

如何更改在 IOS 7 中的 UISearchBar 中定位或隐藏放大镜图标?

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

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

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
向地狱狂奔 2024-09-16 21:39:33

您可以使用

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

You can use

searchBar.setImage(UIImage(), for: .search, state: .normal)
烟火散人牵绊 2024-09-16 21:39:33

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

UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);

Replace the search bar icon with a 1x1 transparent image and offset the text position

UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);
东走西顾 2024-09-16 21:39:33

在 Swift 中,要删除搜索图标,请使用以下命令:

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

要替换为自定义图像,请更改图像上的 UIImage()

In Swift to remove search icon use this:

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

To replace with custom image change UIImage() on your image.

捶死心动 2024-09-16 21:39:33

我在 iOS 12 和 13 上尝试了所有答案 - 但没有一个在这两个版本上都能正常工作。唯一正确的答案如下:

searchField.leftViewMode = .never
if #available(iOS 13.0, *) {
    searchTextPositionAdjustment = UIOffset(horizontal: -20, vertical: 0);
}

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);
}
泪痕残 2024-09-16 21:39:33

您可以子类化 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;
    }
}

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;
    }
}
月朦胧 2024-09-16 21:39:33

swift 5.0,xCode 12 你可以下一步:
searchBar.setImage(UIImage(),用于:.search,状态:.normal)

swift 5.0, xCode 12 you can do next:
searchBar.setImage(UIImage(), for: .search, state: .normal)

记忆之渊 2024-09-16 21:39:33

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

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

拍摄带有 searchIcon.jpg 名称的透明图像并隐藏您的图标
它对我有用

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

疯到世界奔溃 2024-09-16 21:39:33

您无法轻松删除图像,但可以轻松插入 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];

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

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

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

猫烠⑼条掵仅有一顆心 2024-09-16 21:39:33

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

你可以设置的属性:)

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

property u can set :)

神妖 2024-09-16 21:39:33

对于特定的 UISearchBar 实例。

目标-C:

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

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

For a specific UISearchBar instance.

Objective-C:

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

// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];
眼前雾蒙蒙 2024-09-16 21:39:33

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文