How to set background image for UINavigationBar on different iOS Devices(如何在不同的 iOS 设备上为 UINavigationBar 设置背景图片)
问题描述
我想在 UINavigationBar 上设置完整的图像,为此我有:
I want to set full image on UINavigationBar, for this I have:
@2x image (640 x 128)
@3x image (960 x 192)
下面的截图是问题:
请参考这个黄色轮廓.这部分正在切割.
Please refer this yellow outline. This portion is cutting.
我已经写了这段代码来添加图片:
I have written this code to add image:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav-bar-b"),for: .any, barMetrics: .default)
}
请帮助我提供更好的解决方案.
Please help me to provide a better solution.
推荐答案
我已经解决了这个问题:-
I have fixed this issue like this :-
根据设备大小获取导航图像,否则会破坏导航图像.
Take navigation image base on device size otherwise destroyed navigation image.
iPhone 6P =>//1242 × 191 像素
iPhone 6 = >//750 × 128 像素
iPhone 5 = >//640 × 128 像素
iPhone 6P => //1242 × 191 pixels
iPhone 6 = > //750 × 128 pixels
iPhone 5 = > //640 × 128 pixels
func SetNavigationImage()
{
var navBackgroundImage:UIImage!
if IS_IPHONE_6P
{
navBackgroundImage = UIImage(named: "nav-bar-b_1242×191") //1242 × 191 pixels
}else if IS_IPHONE_6
{
navBackgroundImage = UIImage(named: "nav-bar-b_750×128")//750 × 128 pixels
}
else
{
navBackgroundImage = UIImage(named: "nav-bar-b_640×128")//640 × 128 pixels
}
UITabBar.appearance().layer.borderWidth = 0.0
UITabBar.appearance().clipsToBounds = true
UINavigationBar.appearance().isTranslucent = false
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, for:.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = .white
}
var IS_IPHONE_4_OR_LESS = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
var IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
var IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
var IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
这篇关于如何在不同的 iOS 设备上为 UINavigationBar 设置背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不同的 iOS 设备上为 UINavigationBar 设置背景图片
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
