我想在iOS 11搜索栏中嵌入导航栏时更改文本和图标的颜色。所以占位符文本,搜索文本和搜索图标。
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = false
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchBar.placeholder = "Suchen"
searchController.searchBar.tintColor = .white
}
正如您在图像中看到的那样,文本在深蓝色背景上呈灰色,看起来很难看。我希望文字和图标至少是白色的。 (更改蓝色背景颜色也不起作用,请参阅my other question)
唯一有效的方法是改变闪烁光标的颜色和“取消”按钮,这是通过.tintColor属性完成的。
似乎在iOS 10及以下版本中工作的解决方案在iOS 11中似乎不再起作用,因此请仅发布您在iOS 11中工作的解决方案。谢谢。
也许我错过了iOS 11中关于这种“自动样式”的观点。任何帮助都表示赞赏。
我刚刚发现了如何设置其余部分:(在布兰登的帮助下,谢谢!)
“取消”文字:
searchController.searchBar.tintColor = .white
搜索图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
清晰的图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
搜索文字:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
感谢@Brandon的帮助!
占位符:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
白色背景:
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
取自here。
放
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
和
在UISearchBar.appearance().tintColor = UIColor.white
的AppDelegate
。
或者,将它们都放在[UIViewController viewDidLoad:]
中
设置搜索文本颜色
(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
设置搜索占位符颜色
(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).attributedPlaceholder = [NSForegroundColorAttributeName: UIColor.white]
我对Swift 4.x的两分钱,轻轻地清理干净了。
添加控制器或App Delegate:
appearance.backgroundColor = .green
let myFont = UIFont.italicSystemFont(ofSize: 12)
let attribs = [
NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): myFont,
NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red
]
appearance.defaultTextAttributes = attribs
在控制器中:
self.searchBar.barTintColor = .blue
你会得到蓝色背景,绿色搜索栏背景,红色斜体字体:
除了Darko的回答。在我的情况下,我需要获得纯白色搜索文本字段颜色。我还需要一个自定义borderLine和cornerRadius。因此,如果我只是将背景颜色设置为白色,设置自定义角半径和自定义边框线,我就有这样的东西。
问题是搜索栏有一些子视图,我刚删除它们。这是我的代码:
@interface YourViewController () <UISearchBarDelegate, UISearchControllerDelegate>
// your properties
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) UISearchBar *searchBar;
@end
- (void)viewDidLoad {
[super viewDidLoad];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
_searchController.delegate = self;
_searchController.searchBar.delegate = self;
_searchBar = self.searchController.searchBar;
if (@available(iOS 11.0, *)) {
UITextField *searchTextField = [_searchBar valueForKey:@"searchField"];
if (searchTextField != nil) {
searchTextField.layer.cornerRadius = 4.f;
searchTextField.layer.borderWidth = 1.f;
searchTextField.clipsToBounds = YES;
for (UIView *subView in searchTextField.subviews) {
[subView removeFromSuperview];
}
}
// Color for "Cancel" button
_searchBar.tintColor = [UIColor blackColor];
// Add searchController to navgationBar
_navigationItem.searchController = _searchController;
// Hide searchBar when scroll
_navigationItem.hidesSearchBarWhenScrolling = YES;
}
}
现在我有一个带纯白色背景的searchBar,自定义cornerRadius,自定义边框宽度。我也点击时禁用了灰色突出显示。
我尝试了Daroko解决方案,但是当将背景改为纯白色时(我是灰色的)我遇到了问题。我的解决方案是使用setSearchFieldBackgroundImage。我也不想依靠apple struct获取UITextField
if #available(iOS 11.0, *) {
let whiteImage = UIImage(color: UIColor.white, size: CGSize(width: searchController.searchBar.layer.frame.width, height: searchController.searchBar.layer.frame.height))
searchController.searchBar.setSearchFieldBackgroundImage(whiteImage, for: .normal)
self.navigationItem.searchController = searchController
self.navigationItem.hidesSearchBarWhenScrolling = true
}
public extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
} }
我使用了UIImage扩展:Create UIImage with solid color in Swift
此代码更改文本字段的背景颜色
斯威夫特4
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
编辑:青色的UISearchBar样本