菜鸟教程小白 发表于 2022-12-11 19:38:38

ios - iOS 10 中清晰和半透明 UINavigationBar 状态之间的转换


                                            <p><p>从 iOS 10 开始,联系人应用程序通过 <code>UINavigationBar</code> 的实现具有非常有趣的视觉效果。</p>

<p>从 Root ViewController 来看,该栏看起来很正常,但在点击联系人时,播放的动画将只是导航栏的背景淡出以完全清除,半透明效果也缩小为无同时。</p>

<p>过渡完成后,导航栏的后退按钮等控件还在;这意味着它实际上并没有被隐藏。</p>

<p>当接触 ViewController 被弹出时,动画会反向播放,甚至可以通过<code>UINavigationController</code>的'swipe-back'功能来控制:</p>

<p> <a href="/image/1R2St.png" rel="noreferrer noopener nofollow"><img src="/image/1R2St.png" alt="Example of the effect"/></a> </p>

<p>有谁知道如何实现这个功能?我想可以通过继承 <code>UINavigationBar</code> 并手动控制背景 View 的状态来获得这种效果,但我真的希望避免这种情况,因为我发现让第三方导航栏与状态栏不容易。我想知道是否有办法从系统中免费获取它。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <code>UIGestureRecognizer</code> 和 <code>UIVisualEffect</code> 实现从 <code>clear</code> 到 <code>translucent</code> 的导航栏 View 转换。</p>

<p>在<strong>主 VC</strong>:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
}
</code></pre>

<p>在<strong>详细VC</strong>中:</p>

<p>将 <code>UIGestureRecognizerDelegate</code> 实现到您的 <strong>Class</strong> 并使用以下代码。</p>

<pre><code>override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -&gt; Bool {
    let navigationBarBlurView = UIView(frame: CGRect(x:0, y:0, width:view.frame.size.width, height: UIApplication.shared.statusBarFrame.height + (navigationController?.navigationBar.frame.height)!))
    let blurEffect = UIBlurEffect(style: .light) // Set any style you want(.light or .dark) to achieve different effect.
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = navigationBarBlurView.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    navigationBarBlurView.addSubview(blurEffectView)
    view.addSubview(navigationBarBlurView)
    return true
}
</code></pre>

<p><strong>输出:</strong></p>

<p> <a href="/image/JNXww.gif" rel="noreferrer noopener nofollow"><img src="/image/JNXww.gif" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iOS 10 中清晰和半透明 UINavigationBar 状态之间的转换,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43886492/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43886492/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iOS 10 中清晰和半透明 UINavigationBar 状态之间的转换