0

カスタムナビゲーションバーを使用してプロジェクトを実装しようとしていました。ただし、次のコード:

@implementation UINavigationBar (CustomImage)

// Set the navigation bar background
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"TopBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 1)];
}
@end

iOS5では動作しません。Xcode4.2を使用しています。

質問:以前のバージョン(<iOS 5)で上記のコードの使用を損なうことなく、カスタムナビゲーションバーを実装するにはどうすればよいですか?

どんな助けでもいただければ幸いです。:)

4

1 に答える 1

3

私はついにそれを手に入れました...これは他の人がこれを実装するのに役立つかもしれません。

@implementation UINavigationBar (CustomImage)

// Set the navigation bar background
- (UIImage *)barBackground{
    UIImage *image = [UIImage imageNamed:@"TopBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 1)];
    return image;
}

- (void)didMoveToSuperview{
    // Applies to iOS 5
    if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        [self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
    }
}

// This doesn't work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
    // Draw the image
    [[self barBackground] drawInRect:rect];
}
@end
于 2012-06-12T22:48:18.753 に答える