NavigationBar 배경을 스크롤 위치에 따라 변경

6161 단어 iOSSwiftSwift4
NavigationBar의 배경을 imageView 위에서는 투명하게, imageView 아래까지 스크롤 하면 변경하는 방법을 메모


// はじめにNavigationBarを透明に
override func viewDidAppear(_ animated: Bool) {
    navigationController?.navigationBar.tintColor = .white
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
}

// scrollViewのデリゲートメソッド
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // imageViewの下までスクロールしたら NavigationBarの色を変更
    if scrollView.contentOffset.y > imageView.layer.frame.height - (navigationController?.navigationBar.frame.height)! - UIApplication.shared.statusBarFrame.height {
        // NavigationBarの背景色変更
        navigationController?.navigationBar.barTintColor = .white
        navigationController?.navigationBar.tintColor = .black
        navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        navigationController?.navigationBar.shadowImage = nil

    } else {
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.isTranslucent = true
    }
}

좋은 웹페이지 즐겨찾기