iPhone 앱에서 Uitoolbar에 사용자 정의 배경을 줄 수 있습니까?
문제
Uitoolbar에 일반적인 색조 블루/블랙 페이드 아웃이 아닌 이미지에서 사용자 정의 배경을 줄 수 있습니까?
나는보기에 배경을 제공하고 Uitoolbar의 불투명도를 설정하려고했지만 이는 Uibarbuttons의 불투명도에도 영향을 미칩니다.
해결책
내 자신의 질문에 대답합니다 !!! 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
다른 팁
UiToolbar는 uiview에서 상속됩니다. 이것은 단지 나를 위해 효과가있었습니다.
[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
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];
}
이것은 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];
}
이 조각을 추가하십시오 -(void)viewDidLoad{}
[toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Idimmu의 답변을 사용하고 기본값 대신 Barbuttonitems를 색칠하기를 원한다면,이 두 줄의 코드를 카테고리에 추가 할 수 있습니다.
UIColor *color = [UIColor redColor];
self.tintColor = color;
iOS5 이후로 외관 API를 사용할 수 있습니다.
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
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];
}
이것은 나에게 잘 작동합니다.
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
기본적으로 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; }
제휴하지 않습니다 StackOverflow