加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 从UINavigationBar中的按钮中删除闪光效果

发布时间:2020-12-14 19:49:42 所属栏目:百科 来源:网络整理
导读:如何从导航栏上的按钮中删除光泽/光泽效果? 如果我通过使用自定义图像自定义导航栏,按钮不受影响,我可以从中删除效果(线条和光泽),还是为整个按钮定义十六进制颜色代码,甚至为其定义一个自定义图像? 解决方法 我刚刚经历了这个过程.基本上,您需要创建自定
如何从导航栏上的按钮中删除光泽/光泽效果?
如果我通过使用自定义图像自定义导航栏,按钮不受影响,我可以从中删除效果(线条和光泽),还是为整个按钮定义十六进制颜色代码,甚至为其定义一个自定义图像?

解决方法

我刚刚经历了这个过程.基本上,您需要创建自定义的可拉伸图像,并将其用作按钮的背景来摆脱光泽.更换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,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,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;
    }
}

>以下是我使用的可伸缩图像:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读