Can I give a UIToolBar a custom background in my iPhone app?

后端 未结 10 958
误落风尘
误落风尘 2020-12-02 09:00

Is it possible to give a UIToolBar a custom background from an image rather than the usual tinted blue/black fade out?

I\'ve tried giving the view a background and s

相关标签:
10条回答
  • 2020-12-02 09:27

    UIToolbar inherits from UIView. This just worked for me:

    [topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
    
    0 讨论(0)
  • 2020-12-02 09:30

    this one works fine for me:

    ToolbarOptions *tbar = [[ToolbarOptions alloc] init];
    [tbar setToolbarBack:@"footer_bg.png" toolbar:self.toolbarForPicker];
    [tbar release];
    
    #import <Foundation/Foundation.h>
    @interface ToolbarOptions : NSObject {
    
    }
    -(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;
    @end
    
    #import "ToolbarOptions.h"
    
    
    @implementation ToolbarOptions
    
    -(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {   
    // Add Custom Toolbar
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
    iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
    iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    // Add the tab bar controller's view to the window and display.
    if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
        [bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
    else
        [bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
    bottombar.backgroundColor = [UIColor clearColor];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-02 09:32

    To be iOS 5 compliant you can do something like this

    -(void) addCustomToolbar {
    
        // Add Custom Toolbar
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customToolbar.png"]];
        img.frame = CGRectMake(-2, -20, img.frame.size.width+4, img.frame.size.height);
    
        // Add the tab bar controller's view to the window and display.
    
        if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"5.0" ) )
           [self.tabBarController.tabBar insertSubview:img atIndex:1]; // iOS5 atIndex:1
        else
          [self.tabBarController.tabBar insertSubview:img atIndex:0]; // iOS4 atIndex:0
    
        self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
    
        // Override point for customization after application launch.
        [self.window addSubview:tabBarController.view];
    
    }
    
    0 讨论(0)
  • 2020-12-02 09:38

    just add this piece to your -(void)viewDidLoad{}

    [toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-12-02 09:39

    Slightly modified version of loreto's answer, which works for me on ios 4 and 5:

    // Set the background of a toolbar
    +(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar {   
        // Add Custom Toolbar
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
        iv.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
        iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        // Add the tab bar controller's view to the window and display.
        if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
            [toolbar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
        else
            [toolbar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
        toolbar.backgroundColor = [UIColor clearColor];
    }
    
    0 讨论(0)
  • 2020-12-02 09:39

    If you use idimmu's answer and want your barbuttonitems to be colored instead of the defaults, you can add these couple of lines of code as well to your category:

    UIColor *color = [UIColor redColor];
    self.tintColor = color;
    
    0 讨论(0)
提交回复
热议问题