【发布时间】:2021-05-21 01:58:01
【问题描述】:
我想在我的应用中使用 SwiftUI 制作下拉搜索栏,并且必须与 iOS13 兼容,所以我无法使用 ScrollViewReader。我尝试使用Introspect,但我不明白它的作用和工作原理,因为除了下拉刷新之外,文档没有解释如何为 ScrollView 做一些事情。我使用 Introspect for TextField 成为并退出 FirstResponder,它可以工作,但是当我使用 introspect for scrollview 时,该函数仅在滚动视图首次出现时运行。
@State private var willSearch = false
...
.introspectTextField{ textfield in
textfield.returnKeyType = .done
if willSearch {
textfield.becomeFirstResponder()
}else{
textfield.resignFirstResponder()
}
}
// this works when `willSearch` changed value
这就是我的 ScrollView 内省的样子
extension UIScrollView {
var isBouncing: Bool {
return (contentOffset.y + contentInset.top < 0)
}
}
...
.introspectScrollView { scroll in
if scroll.isDragging{
self.willSearch = scroll.isBouncing;
}
}
// this doesn't work
我也尝试通过关注this suggestion 来检测滚动,但效果不是很好,而且我可以听到我的 MacBook 风扇转动的声音比平时大,所以它可能会影响性能。
这大致就是我希望它的行为Pull down search bar as seen in Telegram
【问题讨论】:
标签: ios swift swiftui uikit introspection