从图像创建导航栏按钮项后面的奇怪背景

时间:2013-06-19 20:15:04

标签: ios

只是尝试从图像添加按钮到导航栏。

代码:

UIBarButtonItem *newConvoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"convos_new.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(newConvoInit:)];
self.navigationItem.rightBarButtonItem = newConvoButton;

结果:

1 (它应该只是背景中没有蓝色按钮的暗图像。)

1 个答案:

答案 0 :(得分:2)

这可能对你想要的东西有点过分。但我觉得这会让你的生活变得更轻松。以下内容仅为您提供没有任何UIBarButtonItem属性的图像。

UIImage *menuImage = [UIImage imageNamed:@"navBarMenuButton.png"];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, menuImage.size.width, menuImage.size.height);
[leftButton setBackgroundImage:menuImage forState:UIControlStateNormal];
aController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

此方法实现了一个自定义UIButton,它给出了您正在使用的UIImage的框架,除了您选择的图片添加到UINavigationBar之外,别无其他任何内容。

奖励是,您不必担心重新调整任何内容,以防将来图像发生变化,因为该框架继承自UIImage

祝你好运!

相关问题