更改 UIBarButtonItem 的图像时遇到问题

发布于 2024-10-03 23:05:37 字数 586 浏览 10 评论 0原文

我正在尝试各种方法来更改 UIBarButtonItem 的图像,一旦按下它,但没有任何运气。

// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(@"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:@selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];

// attempt 2
bookmarkButton.image = [UIImage imageNamed:@"bookmarkdelete.png"];
}

还有其他方法可以做到这一点吗?

I'm trying various ways of changing a UIBarButtonItem's image once it has been pressed, without any luck.

// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(@"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:@selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];

// attempt 2
bookmarkButton.image = [UIImage imageNamed:@"bookmarkdelete.png"];
}

Is there another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

像你 2024-10-10 23:05:37

工具栏包含一个数组 - items - 作为属性。因此,将工具栏设置为 IBOutlet 属性后,我必须在该数组中插入一个新按钮。如下所示:

NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"newButton.png"]  style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];

The toolbar contains an array - items - as a property. So after setting up the toolbar as an IBOutlet property, I had to insert a new button into that array.. like this:

NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"newButton.png"]  style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];
变身佩奇 2024-10-10 23:05:37

bookmarkButton 是 UIButton 吗?是否应该通过 (UIButton *)aBarButtonItem.customView 引用而不是直接引用?

如果它是 UIButton,那么您需要根据状态设置图像: - (void)setImage:(UIImage *)image forState:(UIControlState)state

请注意,还有一个 >setBackgroundImage 使用相同的 API(如果您需要的话)。

Is bookmarkButton a UIButton? Should it be referenced via (UIButton *)aBarButtonItem.customView instead of directly?

If it is a UIButton then you'll want to set the image based on state: - (void)setImage:(UIImage *)image forState:(UIControlState)state

Note that there is also a setBackgroundImage with the same API if you want that instead.

浪菊怪哟 2024-10-10 23:05:37

这应该有效

UIImage *normalButtonImage = [UIImage imageNamed:@"TableViewIcon"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"CollectionViewIcon"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
                                     normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(toggleTableView:)
      forControlEvents:UIControlEventTouchDown];
self.toggleMediaView = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setLeftBarButtonItem:self.toggleMediaView];
self.navigationItem.leftBarButtonItem.enabled = NO;

This should work

UIImage *normalButtonImage = [UIImage imageNamed:@"TableViewIcon"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"CollectionViewIcon"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
                                     normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(toggleTableView:)
      forControlEvents:UIControlEventTouchDown];
self.toggleMediaView = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setLeftBarButtonItem:self.toggleMediaView];
self.navigationItem.leftBarButtonItem.enabled = NO;
南冥有猫 2024-10-10 23:05:37

尝试 [bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];

try [bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文