UIBarButtonItem外观setBackgroundImage按钮下的黑线

时间:2012-10-22 12:46:18

标签: iphone ios uibutton uiappearance

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

这是图片:

enter image description here

你可以清楚地看到它,而且非常讨厌。以下是我设置所有图像的方法:

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

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

2 个答案:

答案 0 :(得分:3)

尝试使用[[UIImage imageNamed:@"nav-button"] resizableImageWithCapInsets:...]。看起来你的图像正在变平,因为它比按钮高度短。

答案 1 :(得分: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:@selector(pushInfoViewController)];