for subView in searchBar.subviews {
if let scopeBar = subView as? UISegmentedControl {
scopeBar.backgroundColor = UIColor.blueColor()
}
}
我一直在嘗試上面的代碼,試圖獲得對scopeBar的引用,隨後設置其背景顏色,但我無法獲得參考。它似乎只經過一次循環,意味着搜索欄只有一個子視圖。在調試器中,搜索欄似乎有一個名爲_scopeBar的實例變量,其類型爲(UISegmentedControl *)。UISearchBar範圍欄可以設置背景圖片,但不是它的顏色?
if let topView = searchBar.subviews.first {
for subView in topView.subviews {
if let cancelButton = subView as? UIButton {
cancelButton.tintColor = UIColor.whiteColor()
cancelButton.enabled = true
}
}
}
第二個代碼塊用於訪問搜索欄的cancelButton。