xcode iOS 13导航栏大标题问题

bbmckpt7  于 2023-06-30  发布在  iOS
关注(0)|答案(5)|浏览(155)

我试图显示一个大的Title在一个Navigation bar,但有清晰的背景。向上滚动时,它将是具有模糊效果的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

   }
}

UITableView位于UINavigationController内部。两者都是从故事板通过segue方式。

wribegjk

wribegjk1#

您可以尝试使用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
    }
}

输出:-

mo49yndu

mo49yndu2#

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

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)
}
rslzwgfq

rslzwgfq3#

嘿你怎么在这里是你的代码试图删除这一行

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

  }
 }
nwlqm0z1

nwlqm0z14#

通过故事板之夜改变属性我有一些见解。通常当我卡住的时候,我会把我以编程方式对故事板所做的更改反映出来,而留在代码中的东西通常就是导致我的bug的原因。如果可能的话试试这个

ltqd579y

ltqd579y5#

为了防止导航栏的大标题改变其默认的重字体,我们应该避免修改UINavigationBarAppearance中的字体。

// Fix the issue of transparent navigation when pushing
    if #available(iOS 13.0, *) {
        self.navigationController?.navigationBar.prefersLargeTitles = true
        let style = UINavigationBarAppearance()
        style.configureWithDefaultBackground()
        self.navigationController?.navigationBar.standardAppearance = style
        self.navigationController?.navigationBar.compactAppearance = style
        //Configure Large Style
        let largeStyle = UINavigationBarAppearance()
        largeStyle.configureWithTransparentBackground()
        self.navigationController?.navigationBar.scrollEdgeAppearance = largeStyle
    }

相关问题