gpt4 book ai didi

ios - SwiftUI在Form/Sheet中有多个NavigationLinks-条目保持突出显示

转载 作者:行者123 更新时间:2023-12-03 09:22:21 25 4
gpt4 key购买 nike

我在Xcode 12/iOS 14中遇到问题。在具有NavigationView的工作表中使用多个NavigationLinks会导致NavigationLink条目在返回页面后仍保持突出显示状态。这不仅是模拟器的问题。请参阅随附的GIF:
enter image description here
有人知道如何解决此问题吗?
类似的问题:SwiftUI - NavigationLink cell in a Form stays highlighted after detail pop(但这不是这里的问题)。

struct ContentView: View {

var body: some View {
Text("")
.sheet(isPresented: .constant(true), content: {
NavigationView {
Form {
Section {
NavigationLink("Link to ViewB", destination: ViewB())
}
}
.navigationBarTitle("ViewA")
}
})
}
}

struct ViewB: View {
@State var selection = 0
let screenOptions = ["a", "b", "c"]
var body: some View{
Form {
Section {
NavigationLink("Link to ViewC", destination: ViewC())
}
}
.navigationBarTitle("ViewB")
}
}

struct ViewC: View {
var body: some View{
Form {
Section {
Text("Test")
}
}
.navigationBarTitle("ViewC")
}
}

最佳答案

在工作表中使用NavigationLink时,我也遇到了这个问题。我在iOS 14上的解决方案也是Swizzle didSelectRowAt:UITableView。选择该行后,我将其取消选择。还有更多代码可以检测其是否在工作表中,等等,但这是基本的工作代码:

extension UITableView {

@objc static func swizzleTableView() {

guard self == UITableView.self else {
return
}

let originalTableViewDelegateSelector = #selector(setter: self.delegate)
let swizzledTableViewDelegateSelector = #selector(self.nsh_set(delegate:))

let originalTableViewMethod = class_getInstanceMethod(self, originalTableViewDelegateSelector)
let swizzledTableViewMethod = class_getInstanceMethod(self, swizzledTableViewDelegateSelector)

method_exchangeImplementations(originalTableViewMethod!,
swizzledTableViewMethod!)
}

@objc open func nsh_set(delegate: UITableViewDelegate?) {
nsh_set(delegate: delegate)

guard let delegate = delegate else { return }

let originalDidSelectSelector = #selector(delegate.tableView(_:didSelectRowAt:))
let swizzleDidSelectSelector = #selector(self.tableView(_:didSelectRowAt:))

let swizzleMethod = class_getInstanceMethod(UITableView.self, swizzleDidSelectSelector)
let didAddMethod = class_addMethod(type(of: delegate), swizzleDidSelectSelector, method_getImplementation(swizzleMethod!), method_getTypeEncoding(swizzleMethod!))

if didAddMethod {
let didSelectOriginalMethod = class_getInstanceMethod(type(of: delegate), NSSelectorFromString("tableView:didSelectRowAt:"))
let didSelectSwizzledMethod = class_getInstanceMethod(type(of: delegate), originalDidSelectSelector)
if didSelectOriginalMethod != nil && didSelectSwizzledMethod != nil {
method_exchangeImplementations(didSelectOriginalMethod!, didSelectSwizzledMethod!)
}
}
}

@objc open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView(tableView, didSelectRowAt: indexPath)

// This is specifically to fix a bug in SwiftUI, where a NavigationLink is
// not de-selecting itself inside a sheet.
tableView.deselectRow(at: indexPath,
animated: true)

}
}
(原始的Swizzle代码来自 https://stackoverflow.com/a/59262109/127853,此代码示例仅添加了 deselectRow调用。)
不要忘记在某个地方调用 UITableView.swizzleTableView(),例如 application:didFinishLaunchingWithOptions:

关于ios - SwiftUI在Form/Sheet中有多个NavigationLinks-条目保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63945077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com