use*_*428 4 uisearchbar ios swift
我试图在Swift中设置搜索栏旁边的取消按钮的文本颜色.这是我的代码:
func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
self.searchDisplayController?.searchBar.showsCancelButton = true
var cancelButton: UIButton
var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as! UIView
for subView in topView.subviews {
if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
cancelButton = subView as! UIButton
cancelButton.setTitleColor(UIColorFromRGB(0x0096FF), forState: UIControlState.Selected)
cancelButton.setTitleColor(UIColorFromRGB(0x0096FF), forState: UIControlState.Normal)
}
}
}
Run Code Online (Sandbox Code Playgroud)
它适用于突出显示的状态,但不适用于正常状态.我知道在Objective-C中我可以使用appearanceWhenContainedIn但在Swift中不存在.
有任何想法吗?
Mr.*_*ean 12
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
for ob: UIView in ((searchBar.subviews[0] )).subviews {
if let z = ob as? UIButton {
let btn: UIButton = z
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
}
}
}
Run Code Online (Sandbox Code Playgroud)
它是UISearchBarDelegate的委托方法:)
| 归档时间: |
|
| 查看次数: |
3236 次 |
| 最近记录: |