Custom background image on UIToolbar in IOS5 SDK

后端 未结 5 513
轮回少年
轮回少年 2020-12-12 22:35

Downloaded IOS5 SDK yesterday, and this code which I use to set my UIToolbar\'s background to a custom image stopped working. If I set the target to IOS4.3 and below it stil

5条回答
  •  佛祖请我去吃肉
    2020-12-12 23:18

    This is along the same line as Simone's answer, but works for iOS 5 and iOS < 5. This is what I'm using in app. You need to call [UINavigationBar setupIos5PlusNavBarImage] somewhere in your app initialization (applicationDidFinishLaunching: is a good candidate). On iOS 5+, setupIos5PlusNavBarImage will use the new UIAppearance protocol to set the background and the drawRect override will be ignored. On iOS < 5, setupIos5PlusNavBarImage will basically be a no-op and the drawRect will handle drawing the image.

    Interface:

    @interface UINavigationBar (CustomNavigationBar)
    
    + (void) setupIos5PlusNavBarImage;
    
    - (void) drawRect: (CGRect) rect;
    
    @end
    

    Implementation:

    @implementation UINavigationBar (CustomNavigationBar)
    
    + (void) setupIos5PlusNavBarImage
    {
        if ([UINavigationBar respondsToSelector: @selector(appearance)])
        {
            [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"menuBar.png"] forBarMetrics: UIBarMetricsDefault];
        }
    }
    
    - (void) drawRect: (CGRect) rect
    {
        UIImage* img = [UIImage imageNamed: @"menuBar.png"];
        [img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    
    @end
    

提交回复
热议问题