5

我正在为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
    }()

有解决方法吗?

4

2 回答 2

2

花了我一段时间,但您可以使用 UIImage 属性withBaselineOffset(fromBottom: CGFloat)

例子:

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

花了我几个小时,但我在 SwiftUI 中的自定义 tabBarController 上遇到了这个问题。这么晚才回复很抱歉。

于 2021-06-10T18:11:37.080 回答
0

您需要使用 UIImage 的 - withAlignmentRectInsets(_ alignmentInsets: UIEdgeInsets)属性,才能成功设置 SFSymbols 的插图。在您的情况下使用以下代码:

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

苹果文档 withAlignmentRectInsets

于 2022-02-03T16:45:57.353 回答