LargeTitles UIScrollView 不支持多个观察者实现 _scrollViewWillEndDraggingWithVelocity:targetContentOffset [英] LargeTitles UIScrollView does not support multiple observers implementing _scrollViewWillEndDraggingWithVelocity:targetContentOffset

查看:56
本文介绍了LargeTitles UIScrollView 不支持多个观察者实现 _scrollViewWillEndDraggingWithVelocity:targetContentOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在我的应用中实现了大标题:

I have implemented large titles in my app with the following code:

if #available(iOS 11.0, *) {
            navigationController?.navigationBar.prefersLargeTitles = true
            navigationItem.largeTitleDisplayMode = .always
        } else {
            // Fallback on earlier versions
        }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y <= 0 {
        if #available(iOS 11.0, *) {
            self.navigationItem.largeTitleDisplayMode = .always
        } else {
            // Fallback on earlier versions
        }
    } else {
        if #available(iOS 11.0, *) {
            self.navigationItem.largeTitleDisplayMode = .never
        } else {
            // Fallback on earlier versions
        }
    }
    self.navigationController?.navigationBar.setNeedsLayout()
    self.view.setNeedsLayout()
    UIView.animate(withDuration: 0.01, animations: {
        self.navigationController?.navigationBar.layoutIfNeeded()
        self.view.layoutIfNeeded()
    })
}

我能够在标签栏上的视图之间成功切换,但是当我将视图推送到标签栏控制器的顶部,然后使用以下代码将其弹出时:

I am able to successfully toggle between views on a tabbar but when I push a view ontop of the tabbar controller and then pop it off using this code:

_ = self.navigationController?.popViewController(animated: true)

当我再次在标签栏上的视图之间切换时,我遇到了这个崩溃:由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:错误:UIScrollView 不支持多个观察者实现 _scrollViewWillEndDraggingWithVelocity:targetContentOffset:"

I get this crash when I toggle between views on the tabbar again: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'ERROR: UIScrollView does not support multiple observers implementing _scrollViewWillEndDraggingWithVelocity:targetContentOffset:'

推荐答案

这不是解决方案,而是您需要在代码中调查的潜在问题.我收到了同样的错误消息(UIScrollView 不支持实现 _scrollViewWillEndDraggingWithVelocity:targetContentOffset 的多个观察者),我注意到我做错了什么.我在使用 NavigationView 的 SwiftUI 应用程序中收到此错误消息.

This is not a solution, but a potential thing that you need to investigate in your code. I got this same error message (UIScrollView does not support multiple observers implementing _scrollViewWillEndDraggingWithVelocity:targetContentOffset) and I noticed I was doing something incorrectly. I got this error message in a SwiftUI app using NavigationView.

我犯的错误是 ParentView 在根目录下有一个 Navigation View.使用 NavigationLink 我正在移动到 ChildView,它也有一个 NavigationView 作为根.这是它在代码中的样子:

The mistake I had made was that ParentView had a Navigation View at the root. Using a NavigationLink I was moving to ChildView, which also had a NavigationView as the root. Here's what it looked like in code:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ParentView()
        }
    }
}

struct ParentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: ChildView()) {
                    Text("Parent view")
                }
            }
            .navigationTitle("Parent")
        }
    }
}

struct ChildView: View {
    var body: some View {
        List {
            ForEach(0 ..< 5) { _ in
                Text("Child view")
            }
        }
        .navigationTitle("Child")
    }
}

最初这就是 ChildView 的样子:

Initially this is what ChildView looked like:

struct ChildView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0 ..< 5) { _ in
                    Text("Second screen")
                }
            }
            .navigationTitle("Second")
        }
    }
}

请注意我是如何尝试推送一个本身嵌入在 NavigationView 中的视图.如第一个片段所示删除它,处理错误消息.您可以尝试调查一下,也许您只是在 UIKit 而不是 SwiftUI 中犯了同样的错误.

Notice how I was trying to push a view which itself was embedded in a NavigationView. Removing it as shown in the first snippet, took care of the error messages. You can try looking into that, maybe you are doing the same mistake just in UIKit instead of SwiftUI.

这篇关于LargeTitles UIScrollView 不支持多个观察者实现 _scrollViewWillEndDraggingWithVelocity:targetContentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆