谷歌地图自动完成更改颜色

4

我正在寻求改变 GMSAutocompleteViewController 颜色的方法。

这是我当前的界面:

Screenshot

我希望文本框的背景颜色是白色,取消按钮也是白色


1
你尝试过阅读这些与 Stack Overflow 相关的票证吗?https://dev59.com/Q3M_5IYBdhLWcg3whzrx,http://stackoverflow.com/questions/19651374/how-to-change-cancel-button-tint-color-of-uisearchbar-in-ios7 或者 http://stackoverflow.com/questions/27838084/change-uibarbuttonitem-from-uisearchbar - Android Enthusiast
6个回答

6

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)
    }

更多关于可编辑内容的信息可以在这里找到。

感谢上帝他们更新了他们的框架和文档!干杯!


9
这些方法在哪里?"setTextColor","setPlaceholderTextColor","setSearchImageColor"。你有写过任何扩展吗?如果是,请分享一下。 - Umair Afzal

0
最新发布的Google Places SDK for iOS(1.13)在GMSAutocompleteViewController中增加了对自定义颜色的额外支持。在在线指南中有一个很好的概述。

1
你什么也没添加。你依赖于应用程序范围的自定义,这是完全愚蠢的。与其正确地做某事并直接在VC上添加属性,不如这样做。每次我在iOS上使用Google框架时,我都感到困惑,因为它太糟糕了。 - ThibaultV

0

将以下代码添加到didFinishLaunchingWithOptions

UINavigationBar.appearance().tintColor = UIColor.whiteColor()

if #available(iOS 9.0, *) {
   UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
} else {
   // Fallback on earlier versions
}

0

更改 GMSAutocompleteViewController 的颜色

做以下事情:

- 创建一个新的 NSObject 类来设置 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()

这个方法很管用,我自己试过了。


0

我在使用Google Maps API时遇到了同样的问题,通过以下代码解决了:

[[UITextField appearanceWhenContainedIn:[<Object of your Google Maps API class(like GMSAutocompleteViewController)>.searchDisplayController.searchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

在创建 Google Maps API 类对象并展示类之前,请添加上述行。
上面的答案也适用于这种情况,并且值得赞赏,但是那行代码会使搜索栏中的文本默认为黑色。
通过使用我的答案,可以将文本颜色更改为 iOS 中可用的任何自定义颜色。

-1

通过其视图属性,可以访问视图控制器的视图,该属性只是一个常规的UIView。UIView具有backgroundColor属性,它是一个UIColor并控制视图的颜色。

这里是一个更改背景颜色的示例代码:

self.view.backgroundColor = UIColor.yellowColor()

这并没有真正回答他的问题。你需要更具体些。 - jervine10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接