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

后端 未结 10 1001
误落风尘
误落风尘 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: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];
    }
    

提交回复
热议问题