xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.42k stars 504 forks source link

UIBarButtonItemAppearance does not proxy the title property from UIBarItem #7168

Open PureWeen opened 4 years ago

PureWeen commented 4 years ago

Included project is just Xamarin.iOS so moved issues from here https://github.com/xamarin/Xamarin.Forms/issues/7785

Summary

When 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 SetValueForKeybut now it tells me that

ivar is prohibited. This is an application bug.

API Changes

From IOS 13 , SetValueForKey method useing key with _cancelButtonTextcan not work in VS and Xcode. However , xcode has another way to realize it before IOS 9.0:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Cancel"] After IOS 9.0 has the follow way :

[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"Cancel";

Unfortunately , they all not found in VS .

e.g.

  1. Create an Xamarin IOS project , and then add a UISearchBar in StoryBoard ,calls MySearchBar
  2. Set MySearchBar.SetShowsCancelButton(true, true);
  3. Use MySearchBar.SetValueForKey(new NSString("Click"), new NSString("_cancelButtonText"));
    Managed Stacktrace:
=================================================================
      at <unknown> <0xffffffff>
      at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_IntPtr <0x00112>
      at Foundation.NSObject:SetValueForKey <0x002ca>
      at AppiOS3.ViewController:ViewDidAppear <0x00888>
      at <Module>:runtime_invoke_void__this___byte <0x00350>
      at <unknown> <0xffffffff>
      at UIKit.UIApplication:UIApplicationMain <0x00251>
      at UIKit.UIApplication:Main <0x000b2>
      at UIKit.UIApplication:Main <0x00132>
      at AppiOS3.Application:Main <0x00092>
      at <Module>:runtime_invoke_void_object <0x00342>
=================================================================

And only can set TintColor:

UIBarButtonItem.AppearanceWhenContainedIn(typeof(UISearchBar)).TintColor = UIColor.Red; Not found property Textor Titlethe same as Xcode.

Intended Use Case

So link : https://stackoverflow.com/questions/58188633/xamarin-problems-with-uisearchbars-cancel-button-in-ios-13

Sample-Link: https://www.dropbox.com/s/3rmksed1jn3nfmj/AppIOSFour.rar?dl=0

chamons commented 4 years ago

Ok, the crash itself is iOS complaining about use of undocumented views.

However, trying to do it correct appears to hit a bug with our Appearance APIs. I can confirm the weirdness you are seeing.

This ugly code should work for you as a workaround:

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