【问题标题】:UIBarButtonItem setBackButtonTitlePositionAdjustment doesn't workUIBarButtonItem setBackButtonTitlePositionAdjustment 不起作用
【发布时间】:2020-05-09 22:35:30
【问题描述】:

更新

我想在导航栏中的后退按钮中更改箭头和文本之间的偏移量。在我设置之前它工作得很好

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.prefersLargeTitles,因为上述外观仅适用于标准标题吗?
  • @Asperi 我检查了代码,发现它设置在某个地方,但是在 viewDisappear 上它被关闭了,但我会尝试在任何地方关闭它(我今天或明天会这样做) .您能否将其发布为答案,以便帮助我将您的答案标记为正确?
  • hack - 相反,您在图像的一侧添加了一些空间。或者在文本中添加所需的空格。
  • 我需要负间距
  • 我更新了代码,因为我找到了导致问题的行,但不知道如何解决它“appearance.standardAppearance = standardAppearance”

标签: ios swift xcode uibarbuttonitem uiappearance


【解决方案1】:

UINavigationBar.appearance() 指定时会覆盖所有内容并且对所有内容都有自己的设置,因此您需要通过其自己的属性配置偏移量,例如

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

appearance.standardAppearance = standardAppearance
...

所以不需要UIBarButtonItem.appearance() 的部分 - 只需删除它

【讨论】:

  • 我更新了问题文本,因为我发现是哪一行导致了问题,你能再检查一次吗?
【解决方案2】:

您需要在应用程序委托方法“didFinishLaunchingWithOptions”中设置 TitlePositionAdjustment 才能工作。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UIBarButtonItem
    .appearance()
    .setBackButtonTitlePositionAdjustment(
        UIOffset(horizontal: -20, vertical: 0),
        for: .default)
    return true
}

【讨论】:

  • 我想我正在做同样的事情,但不是在 didFinishLaunchingWithOptions 中,我要在今天或明天检查 didFinishLaunchingWithOptions
  • 是的,你也在做同样的事情,但是你需要把代码放在 AppDelegate 中。
  • 好的,我会检查并在这里回复,谢谢您的建议
  • 不,它没有帮助。而且一般在app里什么时候设置Appearance都没有关系,但是如果你在UI Appeared的时候设置,就需要pop(dismiss)这个画面,所以Appearance才会生效
  • 我更新了问题文本,因为我发现是哪一行导致了问题,你能再检查一次吗?
【解决方案3】:

这是我的自定义后退按钮代码

func addBackButton() {
    let backButtonView = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: 44))
    let imageView = UIImageView(image: UIImage(named: "back-arrow"))
    imageView.frame = CGRect(x: -5, y: 11, width: 12, height: 22)
    imageView.image = imageView.image!.withRenderingMode(.alwaysTemplate)
    imageView.tintColor = .fiGreen()
    let label = UILabel(frame: CGRect(x: 10, y: 0, width: 60, height: 44))
    label.textColor = .fiGreen()
    label.text = NSLocalizedString("back", comment: "Back")
    backButtonView.addSubview(imageView)
    backButtonView.addSubview(label)
    backButtonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backButtonClicked)))
    let barButton = UIBarButtonItem(customView: backButtonView)
    navigationItem.leftBarButtonItem = barButton
}    

我认为你可以修改它以获得你需要的偏移量。

【讨论】:

  • 我不想使用 leftBarButtonItem(它与边缘的偏移量不同),我不想将 UIBarButtonItem 与自定义视图一起使用(因为对于不同的 iOS 版本,需要修复系统后退按钮和自定义),我只需要设置backBarButtonItem的偏移量
  • 我更新了问题文本,因为我发现是哪一行导致了问题,你能再检查一次吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
相关资源
最近更新 更多