【问题标题】:iOS 13 Navigation Bar Large Title IssueiOS 13 导航栏大标题问题
【发布时间】:2020-03-14 03:31:28
【问题描述】:

我试图在Navigation bar 中显示一个大的Title,但背景清晰。向上滚动时,它将是具有模糊效果的Navigation bar

这看起来是正确的,但是,当滚动时,动画似乎被破坏了。此外,过渡有时会卡住:

我的代码如下:

UINavigationController:

override func viewDidLoad() {
   super.viewDidLoad()

   if #available(iOS 13.0, *) {

      self.navigationBar.prefersLargeTitles = true

      let style = UINavigationBarAppearance()
      style.configureWithDefaultBackground()

      style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]

      self.navigationBar.standardAppearance = style
      self.navigationBar.compactAppearance = style


      //Configure Large Style
      let largeStyle = UINavigationBarAppearance()
      largeStyle.configureWithTransparentBackground()

      largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]

      self.navigationBar.scrollEdgeAppearance = largeStyle

   }
}

UITableViewUINavigationController 内。两者都来自故事板,通过 segue 方式。

【问题讨论】:

  • 您描述的配置似乎是默认配置,那么为什么不什么也不做呢?另外我无法重现任何故障,所以也许你有其他代码导致它。
  • 你使用的是 UITableViewController 还是 UITableView?

标签: ios swift xcode uinavigationbar uinavigationbarappearance


【解决方案1】:

在视图调试器中,检查导航标题是否超出导航栏范围。 如果是这样的话:

let titleHeight = UIFont.systemFont(ofSize: 28).lineHeight
if titleHeight > self.view.frame.size.height {
   self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, titleHeight + topAndBottomPadding)
}

【讨论】:

  • 这会去哪里?
【解决方案2】:

您可以尝试使用 UITableViewController 来代替 UITableview。我已经尝试使用 UITableViewController 并且它对我来说工作正常。请检查以下设计和代码。

class ViewController: UITableViewController
{
    override func viewDidLoad() {
        super.viewDidLoad()
          if #available(iOS 13.0, *) {
                  self.navigationController?.navigationBar.prefersLargeTitles = true
                  let style = UINavigationBarAppearance()
                  style.configureWithDefaultBackground()
                  style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]
                  self.navigationController?.navigationBar.standardAppearance = style
                  self.navigationController?.navigationBar.compactAppearance = style
                  //Configure Large Style
                  let largeStyle = UINavigationBarAppearance()
                  largeStyle.configureWithTransparentBackground()
                  largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]
                  self.navigationController?.navigationBar.scrollEdgeAppearance = largeStyle
              }
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        // Do any additional setup after loading the view.
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        10
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
}

输出:-

【讨论】:

  • 我的故事板就是这样设置的。我开始发现这种行为是由于包含在具有向下滑动手势的垂直封面模式视图中的所有内容。
  • 如果您设置并以覆盖垂直样式模态显示它,您将看到您得到了我在 OP 中得到的行为。这也出现在 Apple 自己的邮件应用程序 - 新邮件中
【解决方案3】:

通过故事板之夜更改属性我有一些见解。通常当我被卡住时,我也会反映同样的情况 我以编程方式对情节提要所做的更改以及代码中留下的内容通常是导致我的错误的原因。如果可能的话试试这个

【讨论】:

  • 这似乎发生在导航控制器以模态方式呈现时,在新的向下滑动容器中。我相信向下滑动平移手势是罪魁祸首。您可以在 iOS 13 Mail 应用程序的新邮件容器中看到此行为。
【解决方案4】:

嘿,你好吗是你的代码尝试删除这一行

    largeStyle.configureWithTransparentBackground()

你必须配置为白色背景

你的代码:

    override func viewDidLoad() {
    super.viewDidLoad()

    if #available(iOS 13.0, *) {

  self.navigationBar.prefersLargeTitles = true

  let style = UINavigationBarAppearance()
  style.configureWithDefaultBackground()

  style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]

  self.navigationBar.standardAppearance = style
  self.navigationBar.compactAppearance = style


  //Configure Large Style
  let largeStyle = UINavigationBarAppearance()
  largeStyle.configureWithTransparentBackground()

  largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]

  self.navigationBar.scrollEdgeAppearance = largeStyle

  }
 }

【讨论】:

  • 实际上我需要背景在显示为大标题时是透明的。我拥有的表格视图正在使用模糊视图。这就是我必须使用 configureWithTransparentBackground() 的原因。
  • 模糊视图位于标题的背景或表格视图的背景中
  • 后面的table view的背景图。
  • 如果我不做透明背景,对于大标题,系统会使整个导航栏变高。透明时,看起来没有条,只有表格视图顶部的大标题。
猜你喜欢
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多