2

I'd like to implement a nav style like what is found in the "Add Reminder" view controller from Apple's Reminders (iOS 14). I've tried hooking into the scrollview delegate methods but I'm not sure how to change the alpha of the default nav bar background/shadow image.

enter image description here

I've tried changing the nav bar style on scroll and, while that works, it doesn't fade in/out like in the example. That makes me think the answer lies manipulating the alpha value. Thanks in advance!

4

1 回答 1

4

我找到了一个适用于 iOS 14 的(hacky)解决方案(在其他版本中未经测试)。它对 的私有视图结构进行了假设UINavigationBar,因此不能保证它会在未来的 iOS 版本中工作,但它不太可能崩溃 - 应该发生的最坏情况是该栏将无法隐藏,或仅部分隐藏。

假设您将代码放在一个UIViewController子类中,它充当 a或的委托UITableView,以下应该可以工作:UICollectionViewUIScrollView

override func viewDidLoad() {
    super.viewDidLoad()
    // this hides the bar initially
    self.navigationController?.navigationBar.subviews.first?.alpha = 0
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let navigationController = self.navigationController else { return }
    let navBarHeight = navigationController.navigationBar.frame.height
    let threshold: CGFloat = 20 // distance from bar where fade-in begins
    let alpha = (scrollView.contentOffset.y + navBarHeight + threshold) / threshold
    navigationController.navigationBar.subviews.first?.alpha = alpha
}

魔法threshold值有点难以解释,但它基本上是淡入开始时与小节之间的距离。值 20 表示当 scrollView 内容距离为 20 点时,条开始淡入。值 0 意味着当滚动视图内容接触到它时,条形图直接从完全透明变为完全不透明。

于 2021-01-18T00:45:24.643 回答