3

我所有的按钮下面都有一条小黑线,它们位于UIControlStateNormal. 后退按钮和完成按钮不受影响。

这是图像:

在此处输入图像描述

你可以清楚地看到它,它非常烦人。这是我设置所有图像的方式:

[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"nav-button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

我对返回和完成按钮使用相同的技术,这没有问题。

4

2 回答 2

3

尝试使用[[UIImage imageNamed:@"nav-button"] resizableImageWithCapInsets:...]. 似乎您的图像正在平铺,因为它比按钮高度短。

于 2012-10-22T12:51:45.077 回答
2

可能不是你想要的,但这就是我处理这个的方式:

+ (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:@"navbarbutton"
                         selectedImageNamed:@"navbarbutton_active"
                               leftCapWidth:5.0f
                                 edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f)
                                      title:title
                                     target:target
                                   selector:selector];
}

=> self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBarButtonWithTitle:@"Info" target:self 选择器:@selector(pushInfoViewController)];

于 2012-10-22T12:59:18.577 回答