Search code examples
iosswiftgoogle-mapsgoogle-places-apigmsplacepicker

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


Solution

  • 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;