0

簡単な質問: アプリのメイン ビュー コントローラー (ナビゲーション コントローラー内) で、次のようなナビゲーション バーをカスタマイズしています。

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titleImage"]];

UIButton *menuButton = [[UIButton alloc] init];
[menuButton setImage:[UIImage imageNamed:@"menuIcon"] forState:UIControlStateNormal];
[menuButton setFrame:CGRectMake(0, 0, 34, 34)];

UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.rightBarButtonItem = menuItem;

これらの要素 (タイトル ビューと右側のバー ボタン) は、新しいビュー コントローラーをナビゲーション コントローラーにプッシュ アンド ポップする際に、アプリ全体で一貫性を保ちたいと考えています。

もちろん、viewDidLoadナビゲーション スタックにプッシュされるすべてのビュー コントローラーにカスタム アイテムを設定することもできますが、これは、2 つのビュー コントローラー間のアニメーション中に、カスタム アイテムがアニメーション化されたりアニメーション化されたりすることを意味します。私が望むように。

VC から VC に切り替えるときに、ナビゲーション バーでこれらのカスタム要素を維持する方法について何か提案はありますか? ありがとう!

4

1 に答える 1

0

UIButton をサブクラス化する独自のナビゲーション項目クラスを作成し、そのクラスにすべての外観を設定してから、ナビゲーション項目をそれらのカスタム UIButton として設定することができます。

このようなもの:

CustomButton.h 内

#import <UIKit/UIKit.h>

@interface CustomButton : UIButton

@end

次に CustomButton.m で

@implementation CustomButton

- (id)initWithFrame:(CGRect)frame
 {
     self = [super initWithFrame:frame];
     if (self) {
            //Set images etc.
            [menuButton setImage:[UIImage imageNamed:@"menuIcon"] forState:UIControlStateNormal];
            [menuButton setFrame:CGRectMake(0, 0, 34, 34)];
        }
        return self;
    }


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

次に、viewController で:

#import CustomButton.h

- (void)viewDidLoad
{
     CustomButton *button = [[CustomButton alloc]init];
     self.navigationItem.leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
}
于 2013-10-11T18:15:31.617 に答える