将IBAction分配给自定义navigationBarItem

时间:2013-06-26 19:00:55

标签: ios objective-c uinavigationbar custom-view

我正在使用自定义UIViews作为navigationBarItem(就像Facebook在他们的iOS应用程序中所做的那样)。

UIImageView *linkesMenu = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"187menu.png"]];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:linkesMenu];

barItem.action = @selector(MenuSlider:);
self.navigationItem.leftBarButtonItem = barItem;

我试过这个以及其他一些东西,但没有任何作用。

1 个答案:

答案 0 :(得分:0)

首先尝试将图像放入按钮:

UIImage *theImage = [UIImage imageNamed:@"image.png"];
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
theButton.bounds = CGRectMake(0, 0, theImage.size.width, theImage.size.height);
[theButton setImage:theImage forState:UIControlStateNormal];
UIBarButtonItem *theBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:theButton];


[theButton addTarget:self action:@selector(MenuSlider:) forControlEvent:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = barItem;