在TabBar中,系统图像上的UIEdgeInsets不起作用。

7

我正在使用XCode系统图片(SF Symbols)作为UITabBarItem图像。我想要移除已经完成的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
    }()

有没有解决这个问题的方法?


在图像上使用 withAlignmentRectInsets(_ alignmentInsets: UIEdgeInsets)。 - Sayalee Pote
2个回答

7
花了我一些时间,但你可以使用UIImage中的属性 withBaselineOffset(fromBottom:CGFloat) 例子:
UIImage(systemName: "someName")?.withBaselineOffset(fromBottom: 6.0)

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

0
你需要使用UIImage的withAlignmentRectInsets(_ alignmentInsets: UIEdgeInsets)属性,来成功地为SFSymbols设置插图。以下是代码示例:
image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: -6, right: 0)

有关 withAlignmentRectInsets 的苹果文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接