【问题标题】:How do I have navigationbar buttons in the same line as the navigation bar title?如何让导航栏按钮与导航栏标题位于同一行?
【发布时间】:2020-04-24 12:46:34
【问题描述】:

我有两个问题。

  1. 在我的应用程序的主屏幕中,我有一个带有大标题和按钮的导航栏, like so.我希望在导航栏标题的同一行中添加“+”按钮,就像在Messages App.中一样

  2. 我希望我的应用程序详细视图中的导航栏看起来像 Wallet App's card info screen,它具有这种风格的导航栏,标题居中,卡片位于顶部。

我该怎么做呢?

我在 Xcode 11.4 上使用 SwiftUI,目标是 13.4,如果这很重要的话。

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

对于您的第二个问题,您可以使用以下代码使导航栏透明:

public func makeNavigationBarWithoutBorder(color: UIColor = UIColor.clear) {
    self.navigationBar.isTranslucent = true
    self.navigationBar.backgroundColor = UIColor.clear
    let image = UIImage.init(color: color)
    self.navigationBar.setBackgroundImage(image, for: .default)
    self.navigationBar.shadowImage = image
    self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
}

现在添加左右按钮,如下所示。

 self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: Asset.greyCross.image, style: .plain, target: nil, action: nil)
 self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: Asset.greyCross.image, style: .plain, target: nil, action: nil)

希望这将为您提供第二个解决方案。

【讨论】:

    【解决方案2】:

    我也许可以帮助你,但不能使用 SwiftUI。

    您似乎正在使用navigationController?.navigationBar.prefersLargeTitles = true,我认为 Facebook 没有使用它,但如果您希望通过右侧的按钮继续沿着该路径前进,请尝试此操作。

        tableView.addSubview(yourButton)
        yourButton.translatesAutoresizingMaskIntoConstraints = false
        yourButton.bottomAnchor.constraint(equalTo: tableView.topAnchor, constant: -10).isActive = true
        yourButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
    

    2) 图片和标题不属于导航栏。它看起来像一个被限制在它下面的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多