【问题标题】:how set images on buttons of navigation bar?如何在导航栏按钮上设置图像?
【发布时间】:2011-06-22 05:04:03
【问题描述】:

我需要在导航栏的添加、取消和返回按钮上放置图像。我已经完成了添加按钮,但没有获得返回按钮。

即使使用此添加按钮图像,它也会显示蓝色自定义按钮。如何设置?

UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_add_button.png"] 
    style:UIBarButtonSystemItemAdd target:self               
    action:@selector(addNote)];  

【问题讨论】:

  • 您向我们展示了addButton 代码。
  • 这就是我将图像放在添加按钮上的方式。并尝试返回按钮和取消按钮

标签: iphone xcode cocoa-touch uibarbuttonitem


【解决方案1】:
// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);

// Initialize the UIBarButtonItem
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

// Set the Target and Action for aButton
[aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

// Then you can add the aBarButtonItem to the UINavigationBar
...
self.navigationItem.leftBarButtonItem = aBarButtonItem;

// Release buttonImage
[buttonImage release];

【讨论】:

  • 初始化UIBarButtonItem时报错,接口类型不能静态分配
  • * 保留在 bBarButtonItem 的开头
  • @Heena Dave 我的错误请参阅更新的代码。 UIBarButtonItem 是一个对象,我忘了放一个 *.
  • @Heena Dave。您是 iPhone 开发新手吗?
  • 是的,我对 iphone 开发完全陌生。这就是为什么我的问题太核心了。!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
相关资源
最近更新 更多