14

如何在 iOS 7 中向 uibarbuttonitem 提供正常状态和选定/突出显示状态图像?有什么方法可以为 uibarbuttonitem 的正常和选定/突出显示状态提供色调颜色?

我不想使用 uibutton 作为 uibarbuttonitem 的视图!任何优雅的解决方案都将受到高度赞赏。

4

6 回答 6

3

您可以使用 aUIButton作为 a customViewof a并为 a和UIButton 的状态UIBarButtonItem提供两个不同的背景图像。然后当点击按钮时,您可以将状态设置为or 。normalselectedselectedYESNO

// Build right bar button
UIImage *normalButtonImage = [UIImage imageNamed:@"normal-button"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"selected-button"];
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(rightBarButtonPress)
         forControlEvents:UIControlEventTouchDown];
[orientationButton setShowsTouchWhenHighlighted:YES];
[orientationButton setSelected:NO];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setRightBarButtonItem:rightBarButton];

然后单击按钮时的 in 方法,更改状态

- (void)rightBarButtonPress
{
    //toggle selected state of button
    UIBarButtonItem *rightBarButton = self.navigationItem.rightBarButtonItem;
    UIButton *button = (UIButton *)rightBarButton.customView;
    [button setSelected:!button.selected];

    //do whatever else you gotta do
}
于 2015-05-22T19:38:51.563 回答
1

您可以使用UIAppearance 协议参考来做到这一点

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:options forState:UIControlStateNormal];

您可能还必须使用:

[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
于 2013-11-21T15:47:17.840 回答
1

根据这个答案,我做了一些额外的事情,并且在这里为您提供了答案。我有我的自定义 UITabBarController,它在 StoryBoard 文件中与我的 UITabBarController 链接。因此,为了在未选择 TabBar 时移除 iOS 提供的自动色调,我最终以这种方式将其移除。图像可以是各种各样的图像,但只是按照此处推荐的方式。它是这样的:

NSArray *navConArr = self.viewControllers;//self is custom UITabBarController
UINavigationController *naviOne = [navConArr objectAtIndex:0];//I have 3 different tabs, objectAtIndex:0 means the first tab navigation controller
UITabBarItem *naviBtn  = naviOne.tabBarItem;
UIImage *image = [[UIImage imageNamed:@"iconNaviOne"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[naviBtn setSelectedImage:image];
[naviBtn setImage:image];

因此,当从该选项卡移动到另一个选项卡并将其保留为未选择(默认为灰色)色调时,您可以将其设置为提供的图像。值得庆幸的是,这就像一个魅力(:

于 2015-09-04T23:04:02.397 回答
0

斯威夫特 5.x

guard let button = self?.navigationItem.rightBarButtonItem?.customView as? UIButton  else { return }
// button.isSelected = #your condition here
// button.tintColor = #your condition here
于 2021-11-21T07:47:33.393 回答
0

这是Swift 4解决方案:

private var barButtonItem: UIBarButtonItem?

override func viewDidLoad() {
    super.viewDidLoad()
    let barButtonItem = UIBarButtonItem(image: UIImage(named: "YourImage"), style: .plain, target: self, action: #selector(handleBarButtonAction))
    navigationItem.rightBarButtonItem = barButtonItem
    self.barButtonItem = barButtonItem
}

@objc
private func handleBarButtonAction() {
    if barButtonItem?.image == UIImage(named: "YourImage") {
        barButtonItem?.image = UIImage(named: "YourSelectedImage")
    } else {
        barButtonItem?.image = UIImage(named: "YourImage")
    }
}
于 2018-05-03T09:18:19.617 回答
0

AFAIK 没有本地/优雅的解决方案可以做到这一点,我所做的是跟踪按钮的状态并基于此更改 tintColor

@objc func handleButton() {
    if yourButtonIsSelected {
        yourButton.tintColor = unselectedColor
    } else {
        yourButton.tintColor = selectedColor
    }
}

请注意,无法知道是否UIBarButtonItem选择了 a,因此您也需要提供此信息(yourButtonIsSelected在我的示例中)

于 2021-05-16T06:33:05.937 回答