Xamarin-macios: Null UISearchBar when creating a new UISearchController without arguments

Created on 23 May 2018  路  6Comments  路  Source: xamarin/xamarin-macios

Hi! Just encountered an old bug that still seems to be present: creating a UISearchController without arguments leads to a missing UISearchBar inside it.

Steps to Reproduce

var searchController = new UISearchController();

Actual Behavior

searchController.SearchBar is null

Creating the controller with new UISearchController((UIViewController)null) fixes the problem.

Environment

  • iOS 11.3
  • VS4Mac 7.5.1
  • Mono 5.10.1.47

Example Project (If Possible)

SearchControllerBug.zip

Thank you!

enhancement good first issue help wanted iOS

Most helpful comment

I believe this is the _intended_ behavior of the native API, not a bug. Xamarin.iOS is a thin wrapper, we cannot change the underlying behavior of the API.

It's not, it's a bug in iOS. You can construct using UISearchController() in iOS 13, and it will successfully create the UISearchBar.

iOS 12 exhibits the bug.

All 6 comments

Hi,

I believe this is the _intended_ behavior of the native API, not a bug. Xamarin.iOS is a thin wrapper, we cannot change the underlying behavior of the API.

let searchController = UISearchController()
print("Searchbar is: \(searchController.searchBar)") // nil searchBar

let searchController2 = UISearchController(searchResultsController: nil)
print("Searchbar is: \(searchController2.searchBar)")
// Searchbar is: <UISearchBar: 0x7fc23dc0b240; frame = (0 0; 375 56); text = ''; gestureRecognizers = <NSArray: 0x604000443cf0>; layer = <CALayer: 0x604000222da0>>

However there are 2 things I believe we can do here:

  1. We could add an [Advice ("")] attribute to the default constructor (we'd need to add that implicit constructor to the bindings file first). It would mention that it's _recommended_ to use the constructor that takes a UIViewController searchResultsController in order to create/initialize an attached UISearchBar.

  2. We can also add an [Advice ("")] to IntPtr Constructor ([NullAllowed] UIViewController searchResultsController); mentioning that you can pass a null view controller to display the search results in the same view.

PR welcomed 鉂わ笍

Hi, I tried to solve this but I ran into an issue. The file UISearchController.g.cs (where the constructor of UISearchController is) is located in the build folder which is in the .gitignore.
Whenever I "make all -j8 && make install -j8" what I did in my file is cancelled. What should I do ?
Thanks in advance.

@joconte The UISearchController.g.cs file is generated, you must edit the src/uikit.cs file instead: https://github.com/xamarin/xamarin-macios/blob/82a1f814b388c49943cfdaf1eb30c00eafb8958c/src/uikit.cs#L10223

@rolfbjarne Thanks, I am ashamed.

@joconte shouldn't be :)

I believe this is the _intended_ behavior of the native API, not a bug. Xamarin.iOS is a thin wrapper, we cannot change the underlying behavior of the API.

It's not, it's a bug in iOS. You can construct using UISearchController() in iOS 13, and it will successfully create the UISearchBar.

iOS 12 exhibits the bug.

Was this page helpful?
0 / 5 - 0 ratings