【问题标题】:Removing shine effect from the buttons in a UINavigationBar从 UINavigationBar 中的按钮中删除发光效果
【发布时间】:2011-09-05 12:39:37
【问题描述】:

如何从导航栏上的按钮中移除光泽/光泽效果? 如果我通过使用自定义图像自定义导航栏按钮不受影响,我可以从它们中删除效果(线条和光泽),或者为整个按钮定义十六进制颜色代码,甚至为它们定义自定义图像?

【问题讨论】:

标签: iphone objective-c ios xcode uinavigationbar


【解决方案1】:

我刚刚经历了解决这个问题的过程。基本上,您需要创建自定义的可拉伸图像并将它们用作按钮的背景以消除光泽。替换 UINavigationController 中的后退按钮有点困难。为此,我使用 UINavigationControllerDelegate 将默认的后退按钮替换为我的自定义按钮。

这里有一些代码:

  1. 在 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
    
  2. 将按钮添加到您的 UINavigationBar

    UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)];
    self.navigationItem.rightBarButtonItem = logoutButton;
    
  3. 如果您还想替换 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;
        }
    }
    
  4. 这是我正在使用的可拉伸图像:

    • 返回按钮: 按下:
    • 常规按钮: 按下:

【讨论】:

  • 非常感谢贾斯汀的代码。我发现仅仅放入您的示例图像并不能开箱即用(可能是因为它们不是视网膜资产?)。以下是一些我发现对我更有效的示例:cl.ly/image/441k1H1R1J1e(正常 BBI)和cl.ly/image/0K0O3b101P3R(后 BBI)。
【解决方案2】:

要更改后退按钮,无需实现委托方法 uinavigationcontroller。

您只需在设置所需的后退按钮后将 hidesBAckButton 属性设置为 YES,正如 @Justin Gallacher 完美解释的那样。

self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController   selector:@selector(popViewControllerAnimated:)];
self.navigationItem.hidesBackButton = YES;

【讨论】:

  • 这甚至没有必要,设置 leftBarButtonItem 属性将覆盖后退按钮
【解决方案3】:

您必须使用带有图像的自定义按钮,而对图像没有任何光泽效果,您可以通过导航栏摆脱按钮的光泽效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    相关资源
    最近更新 更多