João Fernandes
João Fernandes

Reputation: 531

Xamarin problems with UISearchBar's cancel Button in iOS 13

I'm developing a mobile application using Xamarin.iOS and now i've just updated the Xamarin's latest version, to have compatiblity with the new iOS 13. So far i was changing the text of the UISearchBar cancel button using SetValueForKey but now it tells me that ivar is prohibited. This is an application bug.

Exception

Can you help with alternatives to change the UISearchBar's cancel button text?

EDIT

Thanks to @Junior Jiang - MSFT it was given a quick fix here.

[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
public extern static void void_objc_msgSend_IntPtr (IntPtr receiver, IntPtr selector, IntPtr arg1);

var app = UIBarButtonItem.AppearanceWhenContainedIn (typeof (UISearchBar));
using (var title = new NSString ("Cancel"))
    void_objc_msgSend_IntPtr (app.Handle, ObjCRuntime.Selector.GetHandle ("setTitle:"), title.Handle);
app.TintColor = UIColor.Red;

Upvotes: 1

Views: 341

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

Unfortunately , this method can not works now in IOS 13 .

Even though through OC menthod to do , it also can not work .

[searchBar setValue("Cancel", forKey: "_cancelButtonText")];
//not work in xcode

In Xcode , there is new way to implement it ,

searchBar.showsCancelButton = YES;

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Cancel"];
//before IOS 9

[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"Cancel";
//after IOS 9

However , in Xamarin we can not find the methods, just only can set TintColor :

UIBarButtonItem.AppearanceWhenContainedIn(typeof(UISearchBar)).TintColor = UIColor.White;

The property Text not found in UIBarButtonItem .

Then I have added it to the feature issue in GitHub Xamarin . Here is the link , you can follow it up .

Upvotes: 2

Related Questions