class TestViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var mySearchBar: UISearchBar!
var myOwnInputView = UIView(frame: CGRectMake(100,100,50,50))
overide func viewDidLoad() {
mySearchBar.delegate = self
mySearchBar.inputView = myOwnInputView // errors - "Cannot assign to the result of this expression
}
}
我正在尝试为我的搜索栏分配一个自定义输入 View ,但是根据苹果文档(-抱歉找不到链接),对于 UISearchBar 它似乎是一个只读值。看着this post ,看来 UISearchbar 有多个 subview ,我需要进入 UITextField 部分才能更改输入 View 。但是,我对 Obj-C 不够熟悉,我尝试将代码转换为 Swift 的尝试也没有成功。
最佳答案
这是最简单的方法:
let searchTextField = searchBar.valueForKey("_searchField") as! UITextField
searchTextField.inputView = myOwnInputView
关于swift - 为搜索栏设置输入 View - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565552/