如何在与导航栏标题相同的行中有导航栏按钮?

时间:2020-04-24 12:46:34

标签: ios swift swiftui

我有两个问题。

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

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

我将如何去做?

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

2 个答案:

答案 0 :(得分:0)

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

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)

希望这会给您第二个解决方案。

答案 1 :(得分:0)

尽管没有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)图像和标题不在导航栏中。它看起来像一个受其约束的视图。