【问题标题】:Align UINavigationBar title with button on the right like iOS13 Messages app将 UINavigationBar 标题与右侧的按钮对齐,如 iOS13 Messages 应用程序
【发布时间】:2019-12-23 19:51:34
【问题描述】:

UINavigationBar 中将右栏按钮项与左标题对齐的最佳方法是什么? (现在我在右上角有图标)

【问题讨论】:

  • 你好@Zac,我也面临同样的问题。你找到解决办法了吗?
  • 很遗憾没有@Zept

标签: ios swift uinavigationbar


【解决方案1】:

也许有更正确和最优的解决方案,但它适合我。 不要被语法吓到,我用SnapKit

  1. 创建按钮或视图
lazy private var settingsButton = UIButton().then {
        $0.setImage(Image.settings, for: .normal)
    }
  1. 覆盖viewDidAppear(_ animated: Bool)
navigationController?.navigationBar.subviews.forEach { subview in
    let stringFromClass = NSStringFromClass(subview.classForCoder)
    guard stringFromClass.contains("UINavigationBarLargeTitleView") else { return }
    subview.subviews.forEach { label in
        guard label is UILabel else { return }
        subview.addSubview(settingsButton)
        settingsButton.snp.makeConstraints{
            $0.top.equalTo(label)
            $0.right.equalToSuperview().offset(-14)
            $0.height.width.equalTo(35)
        }
    }
}

  1. 设置UIScrollViewDelegate 并在scrollViewDidScroll 方法中领先于navigationBar 状态,然后隐藏/显示UIBarButtonItem

结果

screenshot

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2017-01-25
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多