如何在 iOS 7 中向 uibarbuttonitem 提供正常状态和选定/突出显示状态图像?有什么方法可以为 uibarbuttonitem 的正常和选定/突出显示状态提供色调颜色?
我不想使用 uibutton 作为 uibarbuttonitem 的视图!任何优雅的解决方案都将受到高度赞赏。
如何在 iOS 7 中向 uibarbuttonitem 提供正常状态和选定/突出显示状态图像?有什么方法可以为 uibarbuttonitem 的正常和选定/突出显示状态提供色调颜色?
我不想使用 uibutton 作为 uibarbuttonitem 的视图!任何优雅的解决方案都将受到高度赞赏。
您可以使用 aUIButton
作为 a customView
of a并为 a和UIButton 的状态UIBarButtonItem
提供两个不同的背景图像。然后当点击按钮时,您可以将状态设置为or 。normal
selected
selected
YES
NO
// 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
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:options forState:UIControlStateNormal];
您可能还必须使用:
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
根据这个答案,我做了一些额外的事情,并且在这里为您提供了答案。我有我的自定义 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];
因此,当从该选项卡移动到另一个选项卡并将其保留为未选择(默认为灰色)色调时,您可以将其设置为提供的图像。值得庆幸的是,这就像一个魅力(:
斯威夫特 5.x
guard let button = self?.navigationItem.rightBarButtonItem?.customView as? UIButton else { return }
// button.isSelected = #your condition here
// button.tintColor = #your condition here
这是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")
}
}
AFAIK 没有本地/优雅的解决方案可以做到这一点,我所做的是跟踪按钮的状态并基于此更改 tintColor
@objc func handleButton() {
if yourButtonIsSelected {
yourButton.tintColor = unselectedColor
} else {
yourButton.tintColor = selectedColor
}
}
请注意,无法知道是否UIBarButtonItem
选择了 a,因此您也需要提供此信息(yourButtonIsSelected
在我的示例中)