gpt4 book ai didi

iphone - 从 UINavigationBar 中的按钮中删除发光效果

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:37 25 4
gpt4 key购买 nike

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

最佳答案

我刚刚经历了解决这个问题的过程。基本上,您需要创建自定义的可拉伸(stretch)图像并将它们用作按钮的背景以消除光泽。替换 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. 这是我正在使用的可拉伸(stretch)图像:

    • 后退按钮:back button按下:enter image description here
    • 常规按钮:enter image description here按下:enter image description here

关于iphone - 从 UINavigationBar 中的按钮中删除发光效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com