所以我有一个.png428x176 px:
我希望将其用作项目处于选定状态时的标签栏背景图像。问题是图像太大。所以现在,我正在探索不同的分辨率后缀:@ 1x,@ 2x,@ 3x。但是,无论我在图像文件名中附加哪一个,该图像对于选项卡栏项目的背景而言仍然太大,尽管随着分辨率提高,它的确会变小。如何确定此图像的正确尺寸?
请记住,这是任何特定UITabBarItem状态处于选定状态时的背景图像。它是指的整个宽度和高度UITabBarItem。我的标签栏将在屏幕的整个宽度上放置三个项目。
许多答案都提供了基于快速的解决方案,但是我要问的是我的图像应以像素为单位的尺寸,换句话说,我应该在包装中包括多少尺寸的图像,以便该图像适合所有屏幕尺寸的标签栏。我可以想象,由于UITabBar默认情况下a期望UIImage其selectionIndicatorImage字段为a UIImageView,而不是a ,因此必须存在一种解决方案,该解决方案不涉及修改the UITabBar并将其UIImageView作为子视图添加,并手动管理图像视图应位于的位置,根据UITabBarItem当前选择的。
我对此问题的回答是如何使UITabBar选择指示器图像填充整个空间?作为参考:
子类UITabBarController
即使旋转也可用于多个设备。
注意事项:
Original。将以下此类分配给情节提要中的UITabBarController或作为基类(如果您通过编程方式进行屏幕处理)。
//
// BaseTabBarController.swift
// MyApp
//
// Created by DRC on 1/27/17.
// Copyright © 2017 PrettyITGirl. All rights reserved.
//
import UIKit
class BaseTabBarController: UITabBarController {
let numberOfTabs: CGFloat = 4
let tabBarHeight: CGFloat = 60
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateSelectionIndicatorImage()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateSelectionIndicatorImage()
}
func updateSelectionIndicatorImage() {
let width = tabBar.bounds.width
var selectionImage = UIImage(named:"myimage.png")
let tabSize = CGSize(width: width/numberOfTabs, height: tabBarHeight)
UIGraphicsBeginImageContext(tabSize)
selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
selectionImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tabBar.selectionIndicatorImage = selectionImage
}
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2351 次 |
| 最近记录: |