【问题标题】:UIBarButtonItem looks bad in ios6UIBarButtonItem 在 ios6 中看起来很糟糕
【发布时间】:2014-02-23 23:44:45
【问题描述】:

我将 NavigationBar 用于我的应用程序。在 iOS7 中,我的导航 leftButtonItem 图标看起来很好(http://prntscr.com/2vaha7)。但在 iOS6 上,它看起来像这样:http://prntscr.com/2vafer

我的代码:

_item2 = [[UINavigationItem alloc] init];

_item2.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sliderMenu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];

[self.navigationBar pushNavigationItem:_item2 animated:NO];

我怎样才能解决这个视觉问题,而不是在 iOS6 中也能正常工作?

最好的问候,

继续。

【问题讨论】:

    标签: ios ios6 ios7 uinavigationbar


    【解决方案1】:

    看:http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6

    和:http://iosdevblog.com/2013/01/26/the-recommended-size-for-custom-uibarbuttonitem/

    您需要为您的按钮创建一个自定义的透明背景图像,并将其设置为 ios 6 条形按钮的背景图像:

    [[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    

    //edit :我首先认为它是一个后退按钮,你需要调整大小,如下所示:

    UIImage *buttonBack30 = [clearImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 6)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    

    我使用以下代码创建了 clearImage,但您可以简单地创建任何颜色的 34x60 图像:

    + (UIImage *)imageWithColor:(UIColor *)color andSize:(CGRect)rect {
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
    

    }

    UIImage *clearImage = [self imageWithColor:[UIColor clearColor] andSize:CGRectMake(0, 0, 34, 60)];
    
        [[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    

    【讨论】:

    • 谢谢,看来它可以解决我的问题,所以我应该在我的应用程序中应用整个 ButtonItem,而不是在 ios6 中正常工作
    • 您只能将其应用于 ios 6 : if ([[[UIDevice currentDevice] systemVersion] floatValue]
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多