UISearchBarのテキストフィールド背景色を変える
↓UISearchBarのサブビュー内にTextFieldnのviewがあるので、その背景色を変えてあげる
RootViewController.swift
import UIKit
class RootViewController: UIViewController , UISearchBarDelegate{
private var mySearchBar: UISearchBar!
override func viewDidLoad() {
mySearchBar = UISearchBar()
mySearchBar.delegate = self
mySearchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44)
mySearchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 32)
mySearchBar.tintColor = UIColor.darkGrayColor()
mySearchBar.text = "テスト"
// 背景色を変える
for subView in mySearchBar.subviews {
for secondSubView in subView.subviews {
if secondSubView.isKindOfClass(UITextField) {
secondSubView.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.3)
}
}
}
self.navigationItem.titleView = mySearchBar
}
}
参考: [stackoverflow] Cannot change search bar background color