[MonoTouch] Category type "overrides"

jhandel at gmail.com jhandel at gmail.com
Fri Mar 30 22:27:53 UTC 2012


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/0828686e/attachment-0001.html>


More information about the MonoTouch mailing list