25

UIToolBarに、通常の色付きの青/黒のフェードアウトではなく、画像からカスタムの背景を与えることは可能ですか?

ビューに背景を与えてUIToolBarの不透明度を設定しようとしましたが、それはその上のUIBarButtonの不透明度にも影響します。

4

10 に答える 10

40

ここで私自身の質問に答えます!!! 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
于 2009-12-21T17:42:23.857 に答える
16

UIToolbarはUIViewから継承します。これは私のためにうまくいきました:

[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
于 2010-08-08T23:23:26.713 に答える
10

ロレートの答えのわずかに変更されたバージョン。これは、iOS4および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];
}
于 2011-07-22T02:10:38.860 に答える
9

これは私がiOS4と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];
}
于 2012-02-23T07:10:36.090 に答える
7

このピースをあなたに追加するだけです-(void)viewDidLoad{}

[toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
于 2012-07-03T01:02:10.297 に答える
2

idimmuの回答を使用して、デフォルトの代わりにbarbuttonitemsに色を付けたい場合は、次の数行のコードをカテゴリに追加することもできます。

UIColor *color = [UIColor redColor];
self.tintColor = color;
于 2010-08-19T04:05:10.387 に答える
2

iOS5以降、AppearanceAPIを使用できます。

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
于 2013-06-05T10:43:09.280 に答える
1

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];

}
于 2011-07-01T09:56:16.553 に答える
0

これは私にとってはうまくいきます:

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
于 2011-08-29T16:44:43.170 に答える
0

これは、基本的にUIToolBarに新しいプロパティを追加するカテゴリで行うことができます。オーバーライドdrawRectは機能しますが、必ずしも将来を保証するものではありません。カスタムの同じ戦略はUINavigationBar、iOS6での動作を停止しました。

これが私がやっている方法です。

.hファイル

@interface UIToolbar(CustomToolbar)

@property(非アトミック、強力)UIView * customBackgroundView;

@終わり

.mファイル

#import "CustomToolbar.h"
#輸入

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){
        [自己addSubview:newView];
    }
}

-(UIView *)customBackgroundView {
    UIView * customBackgroundView = objc_getAssociatedObject(self、&TIToolbarCustomBackgroundImage);

    customBackgroundViewを返します。
}

@終わり

ビューコントローラコードでは、たとえば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;
    }
于 2012-10-28T19:40:30.983 に答える