我创建了一个自定义按钮并将其应用到UIbarbuttonitem。 没有错误但导航栏上没有显示任何内容:( 这是我的代码 -
//create a custom button
UIImage *image = [UIImage imageNamed:@"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[myCustomButton release];
[image release];
[navID release];
任何可以修复我的代码的人? :)
答案 0 :(得分:9)
答案 1 :(得分:1)
要设置具有正确着色的任何图像,请使用UIButton,设置“image for state”,然后使用 customView
创建 UIBarButtonItemUIImage *btnImage = [UIImage imageNamed:@"button"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
_buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
答案 2 :(得分:1)
最简单的方法之一是:
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backk.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtn:)];
self.navigationItem.leftBarButtonItem = button2;
行动方法:
- (IBAction)backBtn:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}