Edouard Barbier
Edouard Barbier

Reputation: 1845

iOS Swift 3 - Did anyone manage to implement Google Place API's new GMSPlacePickerViewController with custom UI?

I'm currently re-building an app that using the PlacePicker from Google Place API to get data that user can then add on my map.

In the past, I used the GMSPlacePicker which is now deprecated since Google release their Place API 2.3. So I'm currently trying to migrate to their new way of using the API via a GMSPlacePickerViewController which according to Google can be implemented with Custom UI.

From Google's documentation:

As the place picker is a normal view controller it can be displayed any way you want. For example, in a popover, fullscreen, pushed onto a navigation stack, or even as part of a custom app UI.

I have managed to make the GMSPlacePickerViewController part of my broader navigation controller which lets me go back and forth inside the place picker's screens, however I haven't yet managed to customise the UI of the search bar that we see on the third screen on the image below.

Issue:

The text inside the SearchBar is black, and I want to make it white but I do not know how to access the searchBar and its properties as everything seems to be abstracted in the framework. I would also like to change the title 'Select a location' (screen 2) to something smaller.

Any thoughts? Thanks in advance

enter image description here

Upvotes: 4

Views: 1381

Answers (2)

Edouard Barbier
Edouard Barbier

Reputation: 1845

Andrew's answer worked for me in the end. I have converted it to Swift 3.0 for those who might need that in the future. You can simply add the following to your AppDelegate.swift file in didFinishLaunchingWithOptions:

Also searchBarTextAttributes is a dictionary so feel free to add more properties in there to customize even further.

let searchBarTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

UITextField.appearance(whenContainedInInstancesOf:[UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

Upvotes: 1

AndrewR
AndrewR

Reputation: 10889

You should be able to change this with UIAppearance proxies, as described in Use the UIAppearance Protocol (GMSPlacePickerViewController uses a GMSAutocompleteViewController internally).

// Color of typed text in the search bar.
NSDictionary *searchBarTextAttributes = @{
                                          NSForegroundColorAttributeName: lightGray,
                                          NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]
                                          };
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
    .defaultTextAttributes = searchBarTextAttributes;

Upvotes: 1

Related Questions