删除 UINavigationBar 中按钮的发光效果

发布于 2024-12-03 03:53:10 字数 103 浏览 0 评论 0原文

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

How can I remove the gloss/shine effect from the buttons on navigation bars?
If I customize the navigation bar by using a custom image the buttons are not affected, can I remove the effect from them (the line and glossing), or define a hex color code for the whole button, or even a custom image for them too?

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

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

发布评论

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

评论(3

清眉祭 2024-12-10 03:53:10

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

下面是一些代码:

  1. 在 UIBarButtonItem 上创建一个类别来创建自定义按钮。这是我的。我使用此类别来自定义常规栏按钮和后退按钮:

    @interface UIBarButtonItem (UIBarButtonItem_customBackground)
    
    + (id) customBarButtonWithTitle:(NSString *)标题目标:(id)目标选择器:(SEL)选择器;
    + (id) customBackButtonWithTitle:(NSString *)标题目标:(id)目标选择器:(SEL)选择器;
    
    @结尾
    
    @实现UIBarButtonItem(UIBarButtonItem_customBackground)
    
    + (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets 标题:(NSString *)标题目标:(id)目标选择器:(SEL)选择器 {
        UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [customButton addTarget:目标操作:ControlEvents的选择器:UIControlEventTouchUpInside];
        customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
        customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f 绿色:0.0f/255.0f 蓝色: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 大小 = CGSizeMake(30.0f, 30.0f);
        如果(标题!= nil){
            大小 = [[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] 比例];
        返回 [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
    }
    
    + (id) customBarButtonWithTitle:(NSString *)标题目标:(id)目标选择器:(SEL)选择器 {
        返回 [self customButtonWithImageNamed:@"navButtonBG.png" 
                     selectedImageNamed:@"navButtonPressedBG.png" 
                           左帽宽度:6.0f 
                             edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f) 
                                  标题:标题 
                                 目标:目标 
                               选择器:选择器];
    }
    
    + (id) customBackButtonWithTitle:(NSString *)标题目标:(id)目标选择器:(SEL)选择器 {    
        返回 [self customButtonWithImageNamed:@"backButtonBG.png" 
                     selectedImageNamed:@"backButtonPressedBG.png" 
                           左帽宽度:12.0f 
                             edgeInsets:UIEdgeInsetsMake(0.0f, 11.0f, 0.0f, 5.0f) 
                                  标题:标题 
                                 目标:目标 
                               选择器:选择器];
    }
    
    @结尾
    
  2. 将按钮添加到您的 UINavigationBar

    UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self 选择器:@selector(logout)];
    self.navigationItem.rightBarButtonItem = logoutButton;
    
  3. 如果您还想替换 UINavigationController 的后退按钮,请设置 UINavigationControllerDelegate 并实现 willShowViewController 方法,如下所示:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController 动画:(BOOL)animated {
        if([navigationController.viewControllers 计数] > 1) {
            UIViewController* backViewController = [navigationController.viewControllers objectAtIndex:(navigationController.viewControllers.count - 2)];
            NSString* backText = backViewController.title;
            UIBarButtonItem* newBackButton = [UIBarButtonItem customBackButtonWithTitle:backText 目标:navigationController 选择器:@selector(popViewControllerAnimated:)];
            viewController.navigationItem.leftBarButtonItem = newBackButton;
            viewController.navigationItem.hidesBackButton = YES;
        }
    }
    
  4. 这是我正在使用的可拉伸图像:

    • 后退按钮: 后退按钮 按下:在此处输入图像描述
    • 常规按钮:在此处输入图像描述 按下:在此处输入图像描述

I just went through the process of figuring this out. Basically, you need to create custom stretchable images and use them as the button's background to get rid of the shine. Replacing the back buttons in a UINavigationController is a bit tougher. For that I used a UINavigationControllerDelegate to replace the default back button with my custom button.

Here's some code:

  1. Create a category on UIBarButtonItem that creates your custom button. Here's mine. I use this category to customize both regular bar buttons and back buttons:

    @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. Add the button to your UINavigationBar

    UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)];
    self.navigationItem.rightBarButtonItem = logoutButton;
    
  3. If you also want to replace the UINavigationController's back buttons, setup a UINavigationControllerDelegate and implement the willShowViewController method like so:

    - (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. Here are the stretchable images I'm using:

    • Back button: back button Pressed: enter image description here
    • Regular button: enter image description here Pressed: enter image description here
看春风乍起 2024-12-10 03:53:10

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

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

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

For changing the back button it is not necessary to implement the delegate method uinavigationcontroller.

You only need set the hidesBAckButton property to YES after setting the desired backbutton as @Justin Gallacher explained perfectly.

self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController   selector:@selector(popViewControllerAnimated:)];
self.navigationItem.hidesBackButton = YES;
£烟消云散 2024-12-10 03:53:10

您必须使用带有图像的自定义按钮,而不会在图像上产生任何光泽效果,这样您就可以消除导航栏中按钮的光泽效果。

you have to use the custom button with images without any gloss effect on the images by which you can get rid of the gloos effect of the button from navbar.

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