使用方法
设置背景图像时setBackgroundImage:forBarMetrics:
它在iPhone 6上呈现如下
如果我将导航栏设置为translucent
,它会正常拉伸。
@implementation OHCNavigationBar
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self setupGradient];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self setupGradient];
}
return self;
}
- (void)setupGradient {
UIImage *gradientImage = [UIImage imageNamed:@"navigationBarBackground.png"];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
@end
答案 0 :(得分:1)
您可以通过将边缘设置为0来设置非重复模式的UINavigationBar
背景图像。
UIImage *gradientImage32 = [[UIImage imageNamed:@"bkg_top_header_default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32
forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsDefault];
更新1:
- (void)setupGradient {
UIImage *gradientImage = [[UIImage imageNamed:@"navigationBarBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
愿这对你有所帮助。
享受编码!!