0

カスタムUINavigationBarを作成するさまざまなオプションを実行していますが、私のアプリはiOS 5以降であるため、次のコードを使用しています。

// Set the background image all UINavigationBars
[[UINavigationBar appearance]  setBackgroundImage:NavigationPortraitBackground 
                                    forBarMetrics:UIBarMetricsDefault];

今、私は右端にカスタム画像ボタンが欲しいのですが、少し迷っています。別の方法でUINavigationBarをサブクラス化し、それにボタンを追加する必要がありますか、それとももっと簡単な方法がありますか?

4

1 に答える 1

1

絶対にこれをサブクラス化します。UINavigationBarはサブクラス化するのは簡単ですが、ナビゲーションコントローラーに入れるのは非常に困難です。私のサブクラス(CFColoredNavigationBar)を紹介します。これには、任意の色の背景が無料で付属しています。

//.h
#import <UIKit/UIKit.h>

@interface CFColoredNavigationBar : UINavigationBar

@property (nonatomic, strong) UIColor *barBackgroundColor;
@property (nonatomic, strong) UIButton *postButton;
@end
//.m
#import "CFColoredNavigationBar.h"

@implementation CFColoredNavigationBar
@synthesize barBackgroundColor = barBackgroundColor_;
@synthesize postButton = postButton_;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)awakeFromNib {
    postButton_ = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.frame)-60, 0, 60.0f, CGRectGetHeight(self.frame))];
    [postButton_ setImage:[UIImage imageNamed:@"Post.png"] forState:UIControlStateNormal];
    [self addSubview:postButton_];
}
- (void)drawRect:(CGRect)rect {
    if (barBackgroundColor_ == nil) {
        barBackgroundColor_ = [UIColor blackColor];
    }
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [barBackgroundColor_ CGColor]);
    CGContextFillRect(context, CGRectInset(self.frame, 0, self.frame.size.height*-2));
}

@end
于 2012-07-02T04:09:44.537 に答える