4

I am searching for change color of GMSAutocompleteViewController

Here is what I am getting:

Screenshot

I want the textfield to be white with white cancel button.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Pan Mluvčí
  • 1,242
  • 2
  • 21
  • 42
  • 1
    Have you tried reading this Stack overflow related tickets? http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color, http://stackoverflow.com/questions/19651374/how-to-change-cancel-button-tint-color-of-uisearchbar-in-ios7 or http://stackoverflow.com/questions/27838084/change-uibarbuttonitem-from-uisearchbar – Android Enthusiast Feb 20 '16 at 20:56

6 Answers6

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

more info about what you can edit here.... Thank god they updated their framework and doc! Cheers!

Pan Mluvčí
  • 1,242
  • 2
  • 21
  • 42
  • 9
    where are these methods ? "setTextColor", "setPlaceholderTextColor", "setSearchImageColor". Have you written a ny extension ? if yes then please share that as well. – Umair Afzal Jul 18 '17 at 12:45
0

The latest release of the Google Places SDK for iOS (1.13) has added extra support for customising colors in GMSAutocompleteViewController. There's a good overview in the online guide.

AndrewR
  • 10,759
  • 10
  • 45
  • 56
  • 1
    You added nothing. You're relying on application-wide customizations, which is completely stupid. Instead of doing something correctly and adding properties directly on the VC. Every time I have to use a Google framework on iOS, I'm baffled by how bad it is. – ThibaultV Jul 20 '18 at 14:34
0

Add following code inside didFinishLaunchingWithOptions

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

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

Change color of GMSAutocompleteViewController

Do following things:

-Create a new NSObject class to set the attributes of UITextField

.h file

@interface HelperClass : NSObject

+(void)setTextFieldAttributes;

@end

.m file

@implementation HelperClass


+(void)setTextFieldAttributes{


    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
     setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [[UITextField appearance] setTintColor:[UIColor whiteColor]];

}

@end

and then in your ControllerClass: GMSAutocompleteViewController

UItextFieldGoogleHelperClass.setTextFieldAttributes()

will do the trick, worked for me.

Adii
  • 23
  • 5
0

I face the same issue while working on Google Maps API and found my solution by the following line of code:

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

Add the line above after you make an object of your Google Maps API class and before you present the class.

The accepted answer above will also work for this scenario and is appreciable, but that line of code will make default black color for the text in the search bar.

By using my answer, one can change the text color to any custom color available in iOS.

Pang
  • 9,564
  • 146
  • 81
  • 122
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
-1

A view controller's view can be accessed through it's view property which is just a regular UIView. UIView have a backgroundColor property which is a UIColor and controls the color of the view.

Here is a sample code for changing background color:

self.view.backgroundColor = UIColor.yellowColor()
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30