UIEdgeInsets无法处理TabBar中的系统映像



我正在为UITabBarItem图像使用XCode系统图像(SF符号(。我想删除我已经删除的UITabBarItem标题。但也可以稍微向下移动UITabBarItem图像。

在过去,当我不使用系统映像时,使用UIEdgeInsets可以很好地工作。但是,这似乎对系统映像没有影响。

代码:

let imageConfiguration = UIImage.SymbolConfiguration(weight: .regular)
let imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)
lazy var activityTabBarItem: UITabBarItem = {
let image = UIImage(systemName: "stopwatch")?.applyingSymbolConfiguration(imageConfiguration)
let tabBarItem = UITabBarItem(title: nil,
image: image,
selectedImage: image)
tabBarItem.imageInsets = imageInsets
return tabBarItem
}()

有解决办法吗?

花了我一段时间,但您可以使用UIImage属性和BaselineOffset(fromBottom:CGFoat(

示例:

UIImage(systemName: "someName")?.withBaselineOffset(fromBottom: 6.0)

花了我几个小时,但我在SwiftUI中的自定义选项卡BarController上遇到了这个问题。很抱歉回复太晚了。

您需要使用UIImage的-withAlignmentRectInsets(_alignmentInsets:UIEdgeInsets(属性,才能成功设置SFSymbol的插入。以下代码在您的情况下:

image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: -6, right: 0)

用于withAlignmentRectInsets 的Apple文档

最新更新