[MonoTouch] Category type "overrides"

jhandel at gmail.com jhandel at gmail.com
Fri Mar 30 22:43:31 UTC 2012


Ok apparently what I needed to do was set some properties ON the bar before I archived and unarchived it.. (don't ask)

this code works for swapping out the implementation of the UINavigationController


UINavigationBar bar = navController.NavigationBar;

bar.TintColor = UIColor.FromRGB (49, 37, 27);

if ((UIDevice.CurrentDevice.SystemVersion.CompareTo ("5") >= 0)) {

UIImage image = UIImage.FromBundle (fileLoc);

bar.SetBackgroundImage (image, UIBarMetrics.Default);

return navController;

}

//int bgViewTag = 4242421;

NSKeyedUnarchiver unarchiver = new NSKeyedUnarchiver (NSKeyedArchiver.ArchivedDataWithRootObject (navController));

Class c = new Class(typeof(CustomUINavigationBar));

unarchiver.SetClass (new Class (typeof(CustomUINavigationBar)), "UINavigationBar");

UINavigationController controller = (UINavigationController)unarchiver.DecodeObject ("root");

((CustomUINavigationBar) controller.NavigationBar).BgFile = fileLoc;

return controller;



The only difference is that I set the UINavigationBar.TintColor.. which was a minor tweak I was adding to this build so that I could at least publish something for testing... Then bam it worked... so ya, SetClass must somehow be dependent on the instance that the archive was built from isn't in a default state..


Anyone else shocked that setting TintColor before creating an Archive (and unarchiving it) makes SetClass work???


Josh

________________________________
From: monotouch-bounces at lists.ximian.com [monotouch-bounces at lists.ximian.com] on behalf of jhandel at gmail.com [jhandel at gmail.com]
Sent: Friday, March 30, 2012 5:27 PM
To: MonoTouch Forums ‎[monotouch at lists.ximian.com]‎
Subject: [MonoTouch] Category type "overrides"

I am at my whits end trying to skin a UINavigationController in iOS 4.X (yes iOS 4.x not 5.X, 5 is easy, that is taken care of).

So time and time again I am brought back to having to somehow override or otherwise mangle the UINavigationBar.. So my question is this :

Can anyone show me how to override (or otherwise mangle) the UINavigationBar AND get it inside the UINavigationController ?  In Objective C, I could Swizzle to swap implementations or use Category to essentially write an override.

If not, then I guess I'll have to roll my own NavigationController from scratch. but I really would prefer to some how register an override as a Category as that is probably the "cleanest" way to go.

FYI: Here is some code I tried with no luck (this should have been an XIBless way to reconstitue the UINavigationController with an instance of my CustomNavigationBar but it didn't fly.


[Register("CustomUINavigationBar")]

public class CustomUINavigationBar:UINavigationBar{

public CustomUINavigationBar(IntPtr handler):base(handler){}

public string BgFile{get;set;}

public override void Draw (RectangleF area)

{

UIImage image = new UIImage (BgFile);

image.Draw(new RectangleF(0,0,this.Frame.Width, this.Frame.Height));

}

}


then this little pare of methods acting like a "factory"


public static UINavigationController CreateNavController (UIViewController rootView, UINavigationControllerDelegate navDelegate, string fileLoc)

{

UINavigationController newController = new UINavigationController ();

newController = styleNavController (newController, fileLoc);

newController.Delegate = navDelegate;

newController.PushViewController(rootView,false);

return newController;

}

public static UINavigationController styleNavController (UINavigationController navController, string fileLoc)

{


if ((UIDevice.CurrentDevice.SystemVersion.CompareTo ("5") >= 0)) {

UINavigationBar bar = navController.NavigationBar;

UIImage image = UIImage.FromBundle (fileLoc);

bar.TintColor = UIColor.FromRGB (49, 37, 27);

bar.SetBackgroundImage (image, UIBarMetrics.Default);

return navController;

}


NSKeyedUnarchiver unarchiver = new NSKeyedUnarchiver (NSKeyedArchiver.ArchivedDataWithRootObject (navController));

Class c = new Class(typeof(CustomUINavigationBar));

unarchiver.SetClass (new Class (typeof(CustomUINavigationBar)), "UINavigationBar");

UINavigationController controller = (UINavigationController)unarchiver.DecodeObject ("root");

((CustomUINavigationBar) controller.NavigationBar).BgFile = fileLoc;

return controller;

}


Despite the effort, the unarchive.SetClass() doesn't seem to actually change the class of "UINavigationBar" like it should. because the DecodeObject call just returns a NavigationController with the default UINavigationBar initialized.




Thanks
Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monotouch/attachments/20120330/d93ec789/attachment-0001.html>


More information about the MonoTouch mailing list