for subView in searchBar.subviews {
if let scopeBar = subView as? UISegmentedControl {
scopeBar.backgroundColor = UIColor.blueColor()
}
}
我一直尝试以上代码来获取对scopeBar的引用,随后设置其背景颜色,但我无法获得引用。它似乎只通过循环一次,这意味着搜索栏仅有一个子视图。在调试器中,搜索栏似乎具有名为_scopeBar的实例变量,类型为(UISegmentedControl*)。
if let topView = searchBar.subviews.first {
for subView in topView.subviews {
if let cancelButton = subView as? UIButton {
cancelButton.tintColor = UIColor.whiteColor()
cancelButton.enabled = true
}
}
}
第二段代码用于访问搜索栏的取消按钮。