在UINavbar右键按钮项目上设置UIImage

时间:2013-09-19 12:22:24

标签: ios storyboard uinavigationbar

我尝试用UIImage设置右键项目:

它推动UINavbar(它是IBOutlet并在故事板中使用)左键和右键,我以编程方式创建。左按钮工作正常而不是右按钮似乎没有初始化。

在标题中:

@property (weak, nonatomic) IBOutlet UINavigationBar *navBar;

我尝试的是:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture", self.userDestFBID]];

NSData *imageData = [NSData dataWithContentsOfURL:url];

UIImage *destProfileImage = [UIImage imageWithData:imageData];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:_destProfileImage style:UIBarButtonItemStyleDone target:self action:@selector(goBack)];

_item = [[UINavigationItem alloc] initWithTitle:nil];

_item.hidesBackButton = YES;

_item.leftBarButtonItem = activityItem;

_item.rightBarButtonItem = rightButton;

[_navBar pushNavigationItem:_item animated:YES];

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

 UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:_destProfileImage] forState:UIControlStateNormal];
  [button addTarget:self action:@selector(action:)forControlEvents:UIControlEventTouchUpInside];
  [button setFrame:CGRectMake(0, 0, 50, 30)];
_item = [[UINavigationItem alloc] initWithTitle:nil];

_item.hidesBackButton = YES;

_item.leftBarButtonItem = activityItem;

_item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
;

[_navBar pushNavigationItem:_item animated:YES];