删除UISearchbar左侧的图像

时间:2010-07-14 10:42:16

标签: iphone uisearchbar

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

21 个答案:

答案 0 :(得分:39)

在iOS 5.0+中,您可以使用

自定义搜索栏图像
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state

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

答案 1 :(得分: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;
    }

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

答案 2 :(得分: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)

答案 3 :(得分:10)

在swift 2.0中执行此操作:

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

答案 4 :(得分:6)

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

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

答案 5 :(得分:5)

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

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

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

答案 6 :(得分:4)

Swift 4解决方案

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

答案 7 :(得分:3)

您可以使用

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

答案 8 :(得分:3)

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

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

答案 9 :(得分:2)

在 Swift 中删除搜索图标使用这个:

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

要替换为自定义图像更改 UIImage() 您的图像。

答案 10 :(得分: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;
    }
}

答案 11 :(得分:1)

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

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

使用searchIcon.jpg名称拍摄透明图像,并隐藏图标 它为我工作

答案 12 :(得分:1)

swift 5.0, xCode 12 你接下来可以做:
searchBar.setImage(UIImage(), for: .search, state: .normal)

答案 13 :(得分:1)

Swift 4解决方案:

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

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

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

答案 14 :(得分:0)

对于特定的UISearchBar实例。

Objective-C:

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

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

答案 15 :(得分:0)

我在iOS 12和13上尝试了所有答案-在两个版本上均无法正常工作。唯一正确的答案如下:

self.view.addSubview(self.view2ForHomeController)
self.view2ForHomeController.fillSuperview()

let xPosition = self.view2ForHomeController.middleView.frame.midX 
let yPostion = self.view2ForHomeController.middleView.frame.midY
print(xPosition) // ------- PRINTS ZERO
print(yPostion)  // ------- PRINTS ZERO

let circle = UIView(frame: CGRect(x: xPosition, y: yPostion, width: 100, height: 100))
circle.backgroundColor = UIColor.green
self.view2ForHomeController.middleView.addSubview(circle)

var progressCircle = CAShapeLayer()
progressCircle.frame = self.view.bounds

let lineWidth: CGFloat = 10
let rectFofOval = CGRect(x: lineWidth / 2, y: lineWidth / 2, width: circle.bounds.width - lineWidth, height: circle.bounds.height - lineWidth)

let circlePath = UIBezierPath(ovalIn: rectFofOval)

progressCircle = CAShapeLayer ()
progressCircle.path = circlePath.cgPath
progressCircle.strokeColor = UIColor.white.cgColor
progressCircle.fillColor = UIColor.clear.cgColor
progressCircle.lineWidth = 10.0
progressCircle.frame = self.view.bounds
progressCircle.lineCap = CAShapeLayerLineCap(rawValue: "round")

circle.layer.addSublayer(progressCircle)
circle.transform = circle.transform.rotated(by: CGFloat.pi/2)

let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1.1
animation.duration = 1
animation.repeatCount = MAXFLOAT
animation.fillMode = CAMediaTimingFillMode.forwards
animation.isRemovedOnCompletion = false
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn)

progressCircle.add(animation, forKey: nil)

答案 16 :(得分:0)

在ios6中

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

属性你可以设置:)

答案 17 :(得分:0)

let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: textfield.frame.size.height))
textfield.leftView = paddingView
textfield.leftViewMode = .always

此解决方案去除了放大镜和填充物。

答案 18 :(得分: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];

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

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

答案 19 :(得分:-1)

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

答案 20 :(得分:-4)

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