2012-09-13

Use iOS5's UIAppearance protocol to custom UINavigationBar background

iOS5的 UIAppearance protocal能幹嘛呢?



這邊有篇中文的參考
http://furnacedigital.blogspot.tw/2011/11/appearance.html

這邊還有一篇英文的參考,有這些Class的示範喔


  • UIActivityIndicatorView
  • UIBarButtonItem
  • UIBarItem
  • UINavigationBar
  • UIPopoverController
  • UIProgressView
  • UISearchBar
  • UISegmentedControl
  • UISlider
  • UISwitch
  • UITabBar
  • UITabBarItem
  • UIToolbar
  • UIView
  • UIViewController

  • http://www.ioslearner.com/uiappearance-proxy-protocol-tutorial/



    想要整個 App 都套同一種客製的UI,就加在 AppDelegate 裡面就行囉!

    - (void)customUI
    {
        // Set the background image for *all* UINavigationBars
        UIImage *navBG = [[UIImage imageNamed:@"navbar-iphone.png"]
                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [[UINavigationBar appearance] setBackgroundImage:navBG
                                           forBarMetrics:UIBarMetricsDefault];

        // Customize navigation bar back button
        UIImage *navBackButtonBG = [[UIImage imageNamed:@"nav-back-button.png"]
                                    resizableImageWithCapInsets:UIEdgeInsetsMake(5, 16, 5, 7)];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:navBackButtonBG
                                                          forState:UIControlStateNormal
                                                        barMetrics:UIBarMetricsDefault];
        
        // Customize navigation bar buttons
        UIImage *navButtonBG = [[UIImage imageNamed:@"nav-button.png"]
                                resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        
        [[UIBarButtonItem appearance] setBackgroundImage:navButtonBG forState:UIControlStateNormal
                                              barMetrics:UIBarMetricsDefault];
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        
        [self customUI];
        
        return YES;
    }