Aug 19, 2010

(iPhone)How to customize UIAlertView

First solution is using addSubview which add subviews into an UIAlertView, just don't forget add newlines in the initWithTitle method's message to make sure you have enough space for subviews. Like :

[[UIAlertView alloc] initWithTitle:@"Title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]

But this solution couldn't change UIAlertView's background, here is second solution that creates an UIAlertView's subclass, overrides several methods (setAlertText, alertText, drawRect, layoutSubviews and show), Joris Kluivers made an excellent sample, check it here : CustomAlert 

(iPhone)How to customize UINavigationBar's background

UINavigationBar has 2 methods to change it's style, barStyle and tintColor, but neither could set a background image for UINavigationBar.

Here is a solution to do that, add following code in your app delegate implementation file (.m) :

@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect {
    UIImage *barImage = [UIImage imageNamed:@"background_image.png"];
    [barImage drawInRect:rect];
}
@end

And change the "background_image.png" to the image what you want. This add a category in all UINavationBar used in your application, and you will see a fancy UINavigationBar.