嵌入到UISearchController中时,UISearchBar.rightView无法正确显示

时间:2019-07-22 23:15:39

标签: ios swift uisearchbar uisearchcontroller

我想创建一个UISearchBar,在右边有两个彼此相邻的按钮。由于默认实现不允许搜索栏中的多个相邻按钮,因此我不得不创建一种变通办法,将搜索栏的rightView属性设置为一个视图,其中两个按钮都添加为子视图。如果我没有将搜索栏嵌入UISearchController中,则此方法可以正常工作,因为按钮可以正常显示。但是,如果我使用具有相同代码的UISearchController(设置rightView的{​​{1}}属性),则不再显示按钮。

这是我设置搜索栏的UISearchController.UISearchBar属性的方法:

rightView

if let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField { textFieldInsideSearchBar.rightView = self.getCameraColorSearchButtons() textFieldInsideSearchBar.rightViewMode = .always } 返回包含两个按钮的视图,如下所示:

getCameraColorSearchButtons

当我使用public func getCameraColorSearchButtons() -> UIView { let customView = UIView() customView.autoresizingMask = [ .flexibleLeftMargin , .flexibleRightMargin ] let btnWidth = 20 var floatWidth = btnWidth let button = UIButton(type: .custom) button.setImage(UIImage(named: "test")!, for: .normal) button.setImage(UIImage(named: "test")!, for: .highlighted) button.tintColor = UIColor.black button.tag = 111 button.frame = CGRect(x: 0, y: 0, width: btnWidth, height: btnWidth) button.addTarget(self, action: #selector(test), for: .touchUpInside) customView.addSubview(button) let colorButton = UIButton(type: .custom) colorButton.setImage(UIImage(named: "test2")!, for: .normal) colorButton.setImage(UIImage(named: "test2")!, for: .highlighted) colorButton.tintColor = UIColor.black colorButton.imageEdgeInsets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4) colorButton.tag = 112 colorButton.addTarget(self, action: #selector(test2), for: .touchUpInside) colorButton.frame = CGRect(x: btnWidth + 4, y: 0, width: btnWidth, height: btnWidth) floatWidth = Int(colorButton.frame.origin.x) + btnWidth customView.addSubview(colorButton) customView.frame = CGRect(x: 0 , y: 0 , width: floatWidth , height: btnWidth) return customView } 时,我通过进行UISearchController来操纵其UISearchBar

0 个答案:

没有答案