Swi*_*att 3 uisearchbar ios swift
for subView in searchBar.subviews {
if let scopeBar = subView as? UISegmentedControl {
scopeBar.backgroundColor = UIColor.blueColor()
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试上面的代码来尝试获得对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
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二段代码用于访问搜索栏的cancelButton。
小智 7
您可以将scopeBar背景图像设置为纯色图像。
首先,您需要根据color创建一个UIImage。为此,您可以在UIImage上创建一个函数或扩展,例如以下代码:
迅捷3
import UIKit
extension UIImage {
class func imageWithColor(color: UIColor) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您只需在范围栏上设置backgroundImage即可:
迅捷3
// Set the scopeBar's background color:
searchBar.scopeBarBackgroundImage = UIImage.imageWithColor(color: UIColor.blue)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1544 次 |
| 最近记录: |