如何从导航栏上的按钮中删除光泽/闪耀效果? 如果我使用自定义图像自定义导航栏按钮不受影响,我可以从中删除效果(线条和光泽),或者为整个按钮定义十六进制颜色代码,甚至也可以为它们定义自定义图像?
答案 0 :(得分:18)
我刚刚完成了解决这个问题的过程。基本上,您需要创建自定义可伸缩图像并将其用作按钮的背景以消除光泽。更换UINavigationController中的后退按钮有点困难。为此,我使用UINavigationControllerDelegate将默认后退按钮替换为我的自定义按钮。
以下是一些代码:
在UIBarButtonItem上创建一个用于创建自定义按钮的类别。这是我的。我使用此类别来自定义常规栏按钮和后退按钮:
@interface UIBarButtonItem (UIBarButtonItem_customBackground)
+ (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
+ (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
@end
@implementation UIBarButtonItem (UIBarButtonItem_customBackground)
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
customButton.titleEdgeInsets = edgeInsets;
UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
[customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
[customButton setTitle:title forState:UIControlStateNormal];
[customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
[customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];
CGSize size = CGSizeMake(30.0f, 30.0f);
if (title != nil) {
size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
}
customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
customButton.layer.shouldRasterize = YES;
customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}
+ (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector {
return [self customButtonWithImageNamed:@"navButtonBG.png"
selectedImageNamed:@"navButtonPressedBG.png"
leftCapWidth:6.0f
edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f)
title:title
target:target
selector:selector];
}
+ (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector {
return [self customButtonWithImageNamed:@"backButtonBG.png"
selectedImageNamed:@"backButtonPressedBG.png"
leftCapWidth:12.0f
edgeInsets:UIEdgeInsetsMake(0.0f, 11.0f, 0.0f, 5.0f)
title:title
target:target
selector:selector];
}
@end
将按钮添加到UINavigationBar
UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)];
self.navigationItem.rightBarButtonItem = logoutButton;
如果您还想替换UINavigationController的后退按钮,请设置UINavigationControllerDelegate并实现willShowViewController方法,如下所示:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if([navigationController.viewControllers count ] > 1) {
UIViewController* backViewController = [navigationController.viewControllers objectAtIndex:(navigationController.viewControllers.count - 2)];
NSString* backText = backViewController.title;
UIBarButtonItem* newBackButton = [UIBarButtonItem customBackButtonWithTitle:backText target:navigationController selector:@selector(popViewControllerAnimated:)];
viewController.navigationItem.leftBarButtonItem = newBackButton;
viewController.navigationItem.hidesBackButton = YES;
}
}
以下是我正在使用的可伸缩图像:
答案 1 :(得分:2)
要更改后退按钮,不必实现委托方法uinavigationcontroller。
您需要在设置所需的后退按钮后将hidesBAckButton属性设置为YES,因为@Justin Gallacher完美地解释了这一点。
self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController selector:@selector(popViewControllerAnimated:)];
self.navigationItem.hidesBackButton = YES;
答案 2 :(得分:0)
你必须使用带有图像的自定义按钮,对图像没有任何光泽效果,你可以通过它来消除导航栏中按钮的全局效果。