右alignmentUISearchBar中的放大镜图标

在Cocoa-Touch中,我们find一个控件UISearchBar 。 我需要对其进行自定义,以便在文本字段中显示的search图标(我们单击它执行相应的操作) 右alignment 。 通常我们发现它左alignment 。 有可能吗? 我已经完成了研发但是找不到

我该怎么做? 有没有什么好的教程,我们可以find它?

不幸的是, UISearchBar并不是直接支持这个function的。 在完成我们想要的事情的时候,我们可能不得不使用可视化或function性妥协的变通方法,或者编写大量的自定义代码。

我已经概述了几种方法,可以接近我们想要的,并可能有用。

访问子视图方法

UISearchBar的文本是一个UITextField ,它是UISearchBar的子视图,通过通常的方式即view.subviews可以访问。

放大镜search图标是UITextFieldleftView属性中的UIImageView 。 我们可以使用以下代码将其切换到右侧:

 // The text within a UISearchView is a UITextField that is a subview of that UISearchView. UITextField *searchField; for (UIView *subview in self.searchBar.subviews) { if ([subview isKindOfClass:[UITextField class]]) { searchField = (UITextField *)subview; break; } } // The icon is accessible through the 'leftView' property of the UITextField. // We set it to the 'rightView' instead. if (searchField) { UIView *searchIcon = searchField.leftView; if ([searchIcon isKindOfClass:[UIImageView class]]) { NSLog(@"aye"); } searchField.rightView = searchIcon; searchField.leftViewMode = UITextFieldViewModeNever; searchField.rightViewMode = UITextFieldViewModeAlways; } 

这给了我们以下结果:

搜索栏1

正如您所看到的图标超出圆angular矩形的边缘,清除图标已经消失。 另外, rightView属性是针对其他内容的,例如search结果button和书签button。 将search图标放在这里可能会以某种方式与此function冲突,即使您可以使用填充的偏移量来适当地定位它。

至less可以使用这种方法将图标解压缩为UIImageView并将其明确定位在您认为合适的地方,就像处理任何其他视图一样,并编写自定义代码来处理轻敲事件等。

定制外观方法

iOS v5.0及更高版本中,您可以使用此处列出的方法自定义search栏的外观。 以下代码利用偏移量来定位UISearchBar的图标和文本:

 [self.searchBar setPositionAdjustment:UIOffsetMake(255, 0) forSearchBarIcon:UISearchBarIconSearch]; [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(-270, 0)]; 

在宽度为320的标准UISearchBar ,它看起来像下面这样:

搜索栏2

这将保持与图标相关联的所有事件function按预期工作,但不幸的是, UITextField是困惑的,尽pipe其宽度,它只显示其文本的一小部分。 如果你能find一种方法让文本显示超出它认为是UITextField的结尾,那么这可能是你最好的select。

我的要求是,search图标应该在右侧,并且只要X图标出现就应该隐藏。

我想出了一个类似的解决scheme,但有点不同,也使用Swift 2和AutoLayout。 基本思想是隐藏左侧视图,用放大镜图像创build新视图,并使用自动布局将其固定在右侧。 然后使用UISearchBarDelegate方法隐藏search图标,只要search栏文本不为空。

 class CustomSearchView: UIView { //the magnifying glass we will put on the right lazy var magnifyingGlass = UIImageView() @IBOutlet weak var searchBar: UISearchBar! func commonInit() { searchBar.delegate = self searchBar.becomeFirstResponder() //find the search bar object inside the subview stack if let searchField = self.searchBar.subviews.firstObject?.subviews.filter( {($0 as? UITextField) != nil }).firstObject as? UITextField { //hides the left magnifying glass icon searchField.leftViewMode = .Never //add a magnifying glass image to the right image view. this can be anything, but we are just using the system default //from the old left view magnifyingGlass.image = (searchField.leftView! as? UIImageView)?.image //add the new view to the stack searchField.addSubview(magnifyingGlass) //use autolayout constraints to pin the view to the right let views = ["view": magnifyingGlass] magnifyingGlass.translatesAutoresizingMaskIntoConstraints = false searchField.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( "H:[view(12)]-|", options: [], metrics: nil, views: views)) searchField.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( "V:[view]-|", options: [], metrics: nil, views: views)) } } //hides whenever text is not empty func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { magnifyingGlass.hidden = searchText != "" } }