代码:
let size = CGSize(width: tabBar.frame.width / CGFloat(TabBarItem.allValues.count),
height: tabBar.frame.height)
let image = UIImage.image(color: Color.action, size: size)
UITabBar.appearance().selectionIndicatorImage = image
在通常的设备上看起来像这样:
在iPhone X上像这样:
iPhone X标签栏项目背景错位的原因是什么?
更新1 :
将代码更改为如下所示后看起来更好但仍然可以解决方法,因为图像未完全占用标签栏项目空间:
var image: UIImage
if DeviceInfo.is5p8Inch {
image = UIImage.image(color: Color.action, size: CGSize(width: 4, height: 4))
image = image.resizableImage(withCapInsets: UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2), resizingMode: .tile)
} else {
let size = CGSize(width: tabBar.frame.width / CGFloat(TabBarItem.allValues.count),
height: tabBar.frame.height)
image = UIImage.image(color: Color.action, size: size)
}
更新2 :
上面的代码来自viewDidLoad
(也来自viewWillAppear
)。 UITabBarController
的子类100%来自代码(没有使用Storyboard / Xibs)。
更新3 :
我们还有一个自定义按钮作为子视图添加到UITabBar,它正确定位。仅selectionIndicatorImage
未对齐...
更新4 :
在viewDidAppear
而非viewDidLoad
或viewWillAppear
中运行上面的原始代码会产生以下结果:
答案 0 :(得分:0)
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tabbar.invalidateIntrinsicContentSize()
}
答案 1 :(得分:0)
试试这个
制作生成彩色图像的图像扩展。
extension UIImage {
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
将该图像设置为tabbarcontroller并设置标签栏的高度为
var tabFrame = self.tabBarController.tabBar.frame
//self.TabBar is IBOutlet of your TabBar
tabFrame.size.height = 48
tabFrame.origin.y = self.window!.frame.size.height - 48
self.tabBarController.tabBar.frame = tabFrame
self.tabBarController.tabBar.autoresizesSubviews = false
self.tabBarController.tabBar.clipsToBounds = true
self.tabBarController.delegate = self
tabNavigation = UINavigationController(rootViewController: self.tabBarController)
tabNavigation.isNavigationBarHidden = true
let numberOfItems = CGFloat((tabBarController.tabBar.items!.count))
let tabBarItemSize = CGSize(width: (tabBarController.tabBar.frame.width) / numberOfItems,
height: (tabBarController.tabBar.frame.height))
tabBarController.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor(red: 90/255.0, green: 43/255.0, blue:123/255.0, alpha: 1.0), size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)
tabBarController.tabBar.frame.size.width = self.window!.frame.width + 4
tabBarController.tabBar.frame.origin.x = -2
答案 2 :(得分:0)
你只需要在延迟后调用它,因为它没有在你的情况下获得正确的tabBar高度并将其设置在self.tabBar中,下面的代码适用于我,我在viewDidload
中做了
Async.main {
let size = CGSize(width: tabBar.frame.width / CGFloat(TabBarItem.allValues.count),
height: tabBar.frame.height)
let image = UIImage.image(color: Color.action, size: size)
UITabBar.appearance().selectionIndicatorImage = image
self.tabBar.selectionIndicatorImage = image // this will change color and height of current tabBar
}