Google 地图自动完成改变颜色
Google maps autocomplete change color
我正在搜索 GMSAutocompleteViewController
的更改颜色
这是我得到的:
我希望文本字段为 白色,带有 白色 取消按钮。
视图控制器的视图可以通过它的视图 属性 访问,这只是一个普通的 UIView。 UIView 有一个 backgroundColor 属性,它是一个 UIColor 并控制视图的颜色。
下面是更改背景颜色的示例代码:
self.view.backgroundColor = UIColor.yellowColor()
用于 iOS (1.13) 的 Google Places SDK 的最新版本在 GMSAutocompleteViewController
中添加了对自定义颜色的额外支持。 the online guide 中有一个很好的概述。
在 didFinishLaunchingWithOptions
中添加以下代码
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
} else {
// Fallback on earlier versions
}
更改 GMSAutocompleteViewController
的颜色
做以下事情:
-新建一个NSObjectclass设置UITextField的属性
.h 文件
@interface HelperClass : NSObject
+(void)setTextFieldAttributes;
@end
.m 文件
@implementation HelperClass
+(void)setTextFieldAttributes{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
}
@end
然后在你的 ControllerClass: GMSAutocompleteViewController
UItextFieldGoogleHelperClass.setTextFieldAttributes()
会成功的,对我有用。
我在处理 Google 地图 API 时遇到了同样的问题,并通过以下代码行找到了我的解决方案:
[[UITextField appearanceWhenContainedIn:[<Object of your Google Maps API class(like GMSAutocompleteViewController)>.searchDisplayController.searchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
在为 Google 地图 API class 创建对象之后,在呈现 class.[= 之前添加上面的行20=]
上面接受的答案也适用于这种情况并且值得赞赏,但是该行代码将使搜索栏中的文本默认为黑色。
通过使用我的回答,可以将文本颜色更改为 iOS 中可用的任何自定义颜色。
Swift 3
@IBAction func searchCityBtn(_ sender: Any) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
UINavigationBar.appearance().barTintColor = UIColor(red: 44.0/255, green: 44.0/255, blue: 49.0/255, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().setTextColor(UIColor.white)
UISearchBar.appearance().barStyle = UIBarStyle.default
UISearchBar.appearance().setPlaceholderTextColor(.white)
UISearchBar.appearance().setSearchImageColor(.white)
self.present(autocompleteController, animated: true, completion: nil)
}
有关您可以编辑的内容的更多信息here...
感谢上帝,他们更新了他们的框架和文档!干杯!
我正在搜索 GMSAutocompleteViewController
这是我得到的:
我希望文本字段为 白色,带有 白色 取消按钮。
视图控制器的视图可以通过它的视图 属性 访问,这只是一个普通的 UIView。 UIView 有一个 backgroundColor 属性,它是一个 UIColor 并控制视图的颜色。
下面是更改背景颜色的示例代码:
self.view.backgroundColor = UIColor.yellowColor()
用于 iOS (1.13) 的 Google Places SDK 的最新版本在 GMSAutocompleteViewController
中添加了对自定义颜色的额外支持。 the online guide 中有一个很好的概述。
在 didFinishLaunchingWithOptions
中添加以下代码UINavigationBar.appearance().tintColor = UIColor.whiteColor()
if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
} else {
// Fallback on earlier versions
}
更改 GMSAutocompleteViewController
做以下事情:
-新建一个NSObjectclass设置UITextField的属性
.h 文件
@interface HelperClass : NSObject
+(void)setTextFieldAttributes;
@end
.m 文件
@implementation HelperClass
+(void)setTextFieldAttributes{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
}
@end
然后在你的 ControllerClass: GMSAutocompleteViewController
UItextFieldGoogleHelperClass.setTextFieldAttributes()
会成功的,对我有用。
我在处理 Google 地图 API 时遇到了同样的问题,并通过以下代码行找到了我的解决方案:
[[UITextField appearanceWhenContainedIn:[<Object of your Google Maps API class(like GMSAutocompleteViewController)>.searchDisplayController.searchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
在为 Google 地图 API class 创建对象之后,在呈现 class.[= 之前添加上面的行20=]
上面接受的答案也适用于这种情况并且值得赞赏,但是该行代码将使搜索栏中的文本默认为黑色。
通过使用我的回答,可以将文本颜色更改为 iOS 中可用的任何自定义颜色。
Swift 3
@IBAction func searchCityBtn(_ sender: Any) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
UINavigationBar.appearance().barTintColor = UIColor(red: 44.0/255, green: 44.0/255, blue: 49.0/255, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().setTextColor(UIColor.white)
UISearchBar.appearance().barStyle = UIBarStyle.default
UISearchBar.appearance().setPlaceholderTextColor(.white)
UISearchBar.appearance().setSearchImageColor(.white)
self.present(autocompleteController, animated: true, completion: nil)
}
有关您可以编辑的内容的更多信息here... 感谢上帝,他们更新了他们的框架和文档!干杯!