NavigationBar 就像 Swift 中的 Apple Music

NavigationBar like Apple Music in Swift

如何像Apple Music中那样做NavigationBar?

我的代码开始时还不错,但是当滚动时它不起作用。任何想法如何去做?我试图找到一些信息,但找不到。

import UIKit


class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

let backImageForDefaultBarMetrics = UIImage(named: "gaga")!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    let navigationBar = navigationController!.navigationBar


    let largeTitleAppearance = UINavigationBarAppearance()


    largeTitleAppearance.configureWithOpaqueBackground()
    largeTitleAppearance.backgroundColor = UIColor.clear
    largeTitleAppearance.shadowColor = UIColor.clear
    largeTitleAppearance.backgroundImage = UIImage(named: "gaga")

    navigationBar.standardAppearance = largeTitleAppearance
    navigationBar.scrollEdgeAppearance = largeTitleAppearance
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
     self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300)
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let height = scrollView.contentOffset.y
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300 - height)

}

看看这个视频 (https://www.youtube.com/watch?v=rNy6aQQYbuY),很容易实现。