navigationView中如何补充UIScrollView?
Posted
技术标签:
【中文标题】navigationView中如何补充UIScrollView?【英文标题】:How to complement UIScrollView in navigationView? 【发布时间】:2021-05-09 16:19:36 【问题描述】:我目前正在使用 SwiftUI 开发应用程序并尝试使用拉动操作刷新数据。
当我在List
中实现该功能时,它可以工作,但如果我在NavigationView
中使用该功能,则该功能不起作用...
// ---OK↓---
RefreshScrollViewTest(refreshControl: self.$refreshControl)
// ---NG↓---
NavigationView
RefreshScrollViewTest(refreshControl: self.$refreshControl)
有没有办法使用NavigationView
中的函数?
代码如下:
import SwiftUI
struct NavigationRefreshTest: View
@State var refreshControl = UIRefreshControl()
var body: some View
// NavigationView
RefreshScrollViewTest(refreshControl: self.$refreshControl)
//
struct RefreshListTest:View
@Binding var refreshControl:UIRefreshControl
var body: some View
List
Text("test1")
Text("test2")
Text("test3")
.onAppear
NotificationCenter.default.addObserver(forName: NSNotification.Name("Update"), object: nil, queue: .main) (_) in
DispatchQueue.main.asyncAfter(deadline: .now() + 1)
print("update...")
struct RefreshScrollViewTest:UIViewRepresentable
func makeCoordinator() ->Coodinator
return RefreshScrollViewTest.Coodinator()
@Binding var refreshControl:UIRefreshControl
func makeUIView(context: Context) -> UIScrollView
let view = UIScrollView()
self.refreshControl.attributedTitle = NSAttributedString(string: "Loding")
self.refreshControl.addTarget(context.coordinator, action: #selector(context.coordinator.update), for: .valueChanged)
view.showsVerticalScrollIndicator = false
view.refreshControl = self.refreshControl
view.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
let child = UIHostingController(rootView: RefreshListTest(refreshControl: self.$refreshControl))
child.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height)
view.addSubview(child.view)
return view
func updateUIView(_ uiView: UIScrollView, context: Context)
class Coodinator:NSObject
@objc func update()
NotificationCenter.default.post(name: NSNotification.Name("Update"), object: nil)
Xcode:版本 12.3
ios:14.0
【问题讨论】:
【参考方案1】:我还没有解决你的问题,但你应该使用这个库SwiftUIRefresh
【讨论】:
感谢您的支持,这是一种简单且有用的方法!就我而言,我将它与一些解决方案一起使用(github.com/siteline/SwiftUIRefresh/issues/15)以上是关于navigationView中如何补充UIScrollView?的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中刷新navigationview的headview内容
如何在 SwiftUI 中隐藏 NavigationView Bar