代码之家  ›  专栏  ›  技术社区  ›  RusHughes

在我的iPhone应用程序中,我可以给一个UItoolbar一个自定义背景吗?

  •  25
  • RusHughes  · 技术社区  · 15 年前

    是否可以从图像中为uitoolbar提供自定义背景,而不是通常的蓝色/黑色淡出?

    我尝试给视图一个背景并设置ui工具栏的不透明度,但这也会影响它上任何ui按钮的不透明度。

    10 回复  |  直到 12 年前
        1
  •  40
  •   RusHughes    15 年前

    回答我自己的问题!!!重写drawrect函数并创建uitoolbar的实现的技巧是:)

        @implementation UIToolbar (CustomImage)
    - (void)drawRect:(CGRect)rect {
        UIImage *image = [UIImage imageNamed: @"nm010400.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    @end
    
        2
  •  16
  •   Ajay Gautam    14 年前

    ui工具栏从uiview继承。这对我很有效:

    [topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
    
        3
  •  10
  •   Chris    13 年前

    略为修改的Loreto答案版本,适用于iOS 4和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];
    }
    
        4
  •  9
  •   Christopher Pickslay    13 年前

    这是我用于iOS 4和5兼容性的方法:

    if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
        [toolbar setBackgroundImage:[UIImage imageNamed:@"toolbar-background"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    } else {
        [toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar-background"]] autorelease] atIndex:0];
    }
    
        5
  •  7
  •   Mateus    12 年前

    把这一块加到你的 -(void)viewDidLoad{}

    [toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
        6
  •  2
  •   dnstevenson    14 年前

    如果您使用IDimmu的答案并希望您的barbuttonities被着色而不是默认值,那么您可以将这两行代码以及您的类别添加到以下代码中:

    UIColor *color = [UIColor redColor];
    self.tintColor = color;
    
        7
  •  2
  •   Jasper    12 年前

    您可以使用外观API,因为IO5:

    [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
        8
  •  1
  •   loretoparisi    13 年前

    要兼容iOS 5,您可以这样做

    -(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];
    
    }
    
        9
  •  0
  •   Melvin    13 年前

    这个对我来说很好:

    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
    
        10
  •  0
  •   Jared Egan    12 年前

    您可以使用一个类别来完成此操作,该类别基本上将新属性添加到uitoolbar。压倒一切 drawRect 可以工作,但不一定是未来的证据。同样的定制策略 UINavigationBar 已停止使用iOS 6。

    我就是这么做的。

    h文件

    @interface UIToolbar (CustomToolbar)
    
    @property (nonatomic, strong) UIView *customBackgroundView;
    
    @end
    

    m文件

    #import "CustomToolbar.h"
    #import 
    
    static char TIToolbarCustomBackgroundImage;
    
    @implementation UIToolbar (CustomToolbar)
    
    - (void)setCustomBackgroundView:(UIView *)newView {
        UIView *oldBackgroundView = [self customBackgroundView];
        [oldBackgroundView removeFromSuperview];
    
        [self willChangeValueForKey:@"tfCustomBackgroundView"];
        objc_setAssociatedObject(self, &TIToolbarCustomBackgroundImage,
                                 newView,
                                 OBJC_ASSOCIATION_RETAIN);
        [self didChangeValueForKey:@"tfCustomBackgroundView"];
    
        if (newView != nil) {
            [self addSubview:newView];
        }
    }
    
    - (UIView *)customBackgroundView {
        UIView *customBackgroundView = objc_getAssociatedObject(self, &TIToolbarCustomBackgroundImage);
    
        return customBackgroundView;
    }
    
    @end
    
    

    在视图控制器代码中,例如 viewDidLoad

        if (self.navigationController.toolbar.customBackgroundView == nil) {
            self.navigationController.toolbar.customBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_background.png"]];
            self.navigationController.toolbar.customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        }