10

NavigationBarの右側にあるボタンとして画像を使用してNavigationBarを作成したいと思います。

以下のスナップショットのようなもの

ここに画像の説明を入力してください

どうすればこれを達成できますか?

4

4 に答える 4

19

お役に立てれば

viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
viewController.navigationItem.rightBarButtonItem = item;    
于 2012-10-29T13:49:59.130 に答える
8

ここにコードがあります。以下のMethodsFromメソッドを呼び出してくださいviewDidLoad

  - (void)addCustomButtonOnNavBar
  {
   UIBarButtonItem * item1= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"passImageNmae1"] style:UIBarButtonItemStylePlain target:self action:@selector(yourButtonAction1)];
   UIBarButtonItem * item2= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"passImageNmae2"] style:UIBarButtonItemStylePlain target:self action:@selector(yourButtonAction2)];
  NSArray * buttonArray =[NSArray arrayWithObjects:item1,item2 ,nil];

   self.navigationItem.rightBarButtonItems =buttonArray;

  }
于 2012-10-29T13:56:08.613 に答える
1
UIView *viewWithButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
//view customization
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:leftButton];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:rightButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:viewWithButtons];
[viewWithButtons release];
于 2012-10-29T13:49:00.973 に答える
1

私はこれにカテゴリを使用します:

UIBarButtonItem + WithImageOnly.h:

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (WithImageOnly)

- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;

@end

UIBarButtonItem + WithImageOnly.m:

#import "UIBarButtonItem+WithImageOnly.h"

@implementation UIBarButtonItem (WithImageOnly)

- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {    
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    frame = CGRectInset(frame, -5, 0);

    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return [self initWithCustomView:button];
}

@end

使用法:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:@"image"] target:self action:@selector(someAction:)]
于 2013-02-01T05:31:11.230 に答える