我通过类别覆盖drawRect,将自定义背景图像添加到我的UINavigationBar。

- ( void )drawRect:( CGRect )rect{
    [[UIImage imageNamed:@"navbar.png"] drawInRect:CGRectMake( 0, self.frame.size.height-44, self.frame.size.width, 44 )];
}

此解决方案似乎工作正常,直到您尝试使用navigationitem.prompt属性。 它没有做出呈现提示的流畅动画,而是突然发生。

有没有关于如何解决这个问题的建议?或者另一种设置背景图像的方法。

PS。我试图避免只是添加背景图像作为子视图,因为视图推/弹时重新排列的方式。我不想在每个视图的viewDidAppear中发送SubviewToBack。

TIA!

编辑:我也试过方法调整,如果我可以让我的图像拉伸,这将工作正常,但由于图像不可伸缩,我需要弄清楚如何利用恰好做转移的动画而不是伸展。

编辑:请参阅下面我的hacky答案

有帮助吗?

解决方案

非常“hacky”,但这是我能做到的最接近我想要的......

基本上当方法调整时,简单地让图像拉伸不适合我,因为我也有圆角。因此,我将角落设为一个单独的透明图像并将其添加为子视图,这样它们就不会受到拉伸动画的影响。

所以我的类别现在看起来像这样..

#import "UINavigationBar+custom.h"

#define cornersTag 1

@implementation UINavigationBar (UINavigationBarCategory)

-(void)setCornersIfNeeded{
 UIImageView *corners = (UIImageView*)[self viewWithTag:cornersTag];
 if(!corners){
  UIImage *img = [[UIImage imageNamed:@"navbar_corners.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
  corners = [[[UIImageView alloc] initWithImage:img] autorelease];
  corners.frame = CGRectMake(0, 0, self.frame.size.width, img.size.height);
  corners.tag = cornersTag;
  [self addSubview:corners];
 }
}

- (void)customDrawRect:( CGRect )rect{
 [self customDrawRect:rect];
 [[UIImage imageNamed:@"navbar.png"] drawInRect:rect]; 
 [self setCornersIfNeeded];
}

@end

有关方法调配的更多信息... http://www.cocoadev.com/index特等?MethodSwizzling iPhone设备上的Method Swizzle

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top