我可以移除UISearchbar
左侧的图像并添加新图像吗?
我可以移除UISearchbar
左侧的图像并添加新图像吗?
iOS 5.0+ 中,您可以使用
searchBar.setImage
自定义搜索栏图像。
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在特定的 UISearchBar
实例中或通过使用 UIAppearance
代理在一大堆实例中应用。
nil
,它实际上并不会删除该图片。 - Senseful[[UISearchBar appearance] setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconSearch];
。 - user289841searchTextPositionAdjustment
在 iOS 12 上不再需要。setPositionAdjustment
将右侧的删除按钮移动到左侧。[UISearchBar appearance]
会破坏其他搜索栏。 - DmitryObjective-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)
// 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;
}
这会隐藏放大镜图标。效果很好。
在Swift 2.0中,做如下操作:
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
valueForKey
?这可能导致应用被拒绝。 - TheCodingArtSwift 4 解决方案
searchBar.setImage(UIImage(), for: .search, state: .normal)
Swift 4解决方案:
searchBar.setImage(UIImage(), for: .search, state: .normal)
您可能也想调整左侧的间隙:
searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
注意:这将从所有包含在UISearchBars
中的UITextFields
中移除左侧图标。
如何在IOS 7中更改UISearchBar中放大镜图标的位置或隐藏它?
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
将搜索栏图标替换为1x1透明图像,并偏移文本位置
UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);
searchTextPositionAdjustment
在 iOS 12 上不需要。顺便问一下,为什么是19而不是20? - Dmitry您可以使用
searchBar.setImage(UIImage(), for: .search, state: .normal)