更新了

我想在导航栏中的“后退”按钮中更改箭头和文本之间的偏移量。直到我设置好
UINavigationBar.appearance().standardAppearance = newAppearance
这是完整的代码:

        let appearance = UINavigationBar.appearance()
        let standardAppearance = UINavigationBarAppearance()
        standardAppearance.configureWithOpaqueBackground()
        standardAppearance.backgroundColor = someColor
        standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
        standardAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
        standardAppearance.shadowColor = navigationShadowColor

        // if remove theses 2 line, offset code works!!!
        appearance.standardAppearance = standardAppearance
        appearance.scrollEdgeAppearance = standardAppearance

        // code to set offset
        UIBarButtonItem
            .appearance()
            .setBackButtonTitlePositionAdjustment(
                UIOffset(horizontal: -20, vertical: 0),
                for: .default)

最佳答案

指定时UINavigationBar.appearance()会覆盖所有内容并为所有内容设置自己的设置,因此您需要通过其自己的属性来配置offset

...
standardAppearance.backButtonAppearance.normal.titlePositionAdjustment =
    UIOffset(horizontal: -20, vertical: 0)

appearance.standardAppearance = standardAppearance
...

因此不需要UIBarButtonItem.appearance()的部分-只需将其删除

10-08 06:11