OGeek|极客世界-中国程序员成长平台

标题: ios - iPhone 6 的 UINavigationBar 背景图片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:56
标题: ios - iPhone 6 的 UINavigationBar 背景图片

我正在尝试向 UINavigationBarController 的 NavigationBar 添加背景图像

[viewController.navigationBar setBackgroundImage:[UIImage imageNamed"NavBarBackground"] forBarMetrics:UIBarMetricsDefault];

在我的 Assets 中,我有一个

Apple 文档中提到了解决方案:

但是

如何处理 iPhone 6 外壳,而无需在我的 setBackgroundImage 方法中使用扩展名命名的另一个图像(仅适用于 iPhone 6)?



Best Answer-推荐答案


问题是 iPhone 6/iPhone 6+ 上的导航栏与所有其他 iPhone 型号相比具有不同的纵横比。 所以在这种情况下,你需要一个不同的图像集。

但是,如果图片只是彩色背景,我建议使用以下代码:

[viewController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor greenColor]] forBarMetrics:UIBarMetricsDefaults

并实现以下方法:

- (UIImage *)imageWithColorUIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

关于ios - iPhone 6 的 UINavigationBar 背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105366/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4