Ios 带显示指示器的快捷菜单列表按钮
我有一个SwiftUI视图,它由一个包含一些项目的列表组成。其中一些是指向其他屏幕的链接(因此我使用Ios 带显示指示器的快捷菜单列表按钮,ios,swift,swiftui,swiftui-list,Ios,Swift,Swiftui,Swiftui List,我有一个SwiftUI视图,它由一个包含一些项目的列表组成。其中一些是指向其他屏幕的链接(因此我使用NavigationLink来执行此操作),另一些是我希望在当前屏幕上执行的操作(例如,显示操作表的按钮) 我正在寻找一种方法,使SwiftUI列表中的按钮显示一个披露指示器(右侧标志上的V形符号,用于导航链接) 这可能吗 例如 当前行为: 通缉行为: 我的答案使用框架,用于: 反思SwiftUI的基础UIKit组件 在这种情况下,它用于在按下NavigationLink后取消选择行 我认为一个
NavigationLink
来执行此操作),另一些是我希望在当前屏幕上执行的操作(例如,显示操作表的按钮)
我正在寻找一种方法,使SwiftUI列表中的按钮
显示一个披露指示器(右侧标志上的V形符号,用于导航链接
)
这可能吗
例如
当前行为:
通缉行为:
我的答案使用框架,用于:
反思SwiftUI的基础UIKit组件
在这种情况下,它用于在按下NavigationLink
后取消选择行
我认为一个带有普通口音颜色且没有导航链接的按钮对用户来说更直观,但如果这是您需要的,那么它就是。下面的答案应该适合您:
import Introspect
import SwiftUI
struct ExampleView: View {
@State private var showingActionSheet = false
@State private var tableView: UITableView?
var body: some View {
NavigationView {
List {
NavigationLink("Navigation Link", destination: Text("xx"))
NavigationLink(
destination: EmptyView(),
isActive: Binding<Bool>(
get: { false },
set: { _ in
showingActionSheet = true
DispatchQueue.main.async {
deselectRows()
}
}
)
) {
Text("Action Sheet")
}
}
.introspectTableView { tableView = $0 }
.listStyle(GroupedListStyle())
.actionSheet(isPresented: $showingActionSheet) {
ActionSheet(title: Text("Title"), buttons: [
.default(Text("Do Something")) { },
.cancel()
])
}
}
}
private func deselectRows() {
if let tableView = tableView, let selectedRow = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: selectedRow, animated: true)
}
}
}
导入内省
导入快捷键
结构示例视图:视图{
@国家私有var showingActionSheet=false
@国家私有var表视图:UITableView?
var body:一些观点{
导航视图{
名单{
导航链接(“导航链接”,目的地:文本(“xx”))
导航链接(
目标:EmptyView(),
isActive:绑定(
获取:{false},
集合:{uu}in
showingActionSheet=true
DispatchQueue.main.async{
取消选择行()
}
}
)
) {
文本(“行动表”)
}
}
.introspectTableView{tableView=$0}
.listStyle(GroupedListStyle())
.actionSheet(显示:$showingActionSheet){
操作表(标题:文本(“标题”),按钮:[
.default(文本(“做某事”){},
.取消
])
}
}
}
private func取消选择行(){
如果让tableView=tableView,则让selectedRow=tableView.indexPathForSelectedRow{
tableView.DeceloseRow(at:selectedRow,动画:true)
}
}
}
可能的方法是使用定制的V形标志划行,如下面的演示(使用Xcode 12.1/iOS 14.1测试)
import Introspect
import SwiftUI
struct ExampleView: View {
@State private var showingActionSheet = false
@State private var tableView: UITableView?
var body: some View {
NavigationView {
List {
NavigationLink("Navigation Link", destination: Text("xx"))
NavigationLink(
destination: EmptyView(),
isActive: Binding<Bool>(
get: { false },
set: { _ in
showingActionSheet = true
DispatchQueue.main.async {
deselectRows()
}
}
)
) {
Text("Action Sheet")
}
}
.introspectTableView { tableView = $0 }
.listStyle(GroupedListStyle())
.actionSheet(isPresented: $showingActionSheet) {
ActionSheet(title: Text("Title"), buttons: [
.default(Text("Do Something")) { },
.cancel()
])
}
}
}
private func deselectRows() {
if let tableView = tableView, let selectedRow = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: selectedRow, animated: true)
}
}
}
struct ExampleView: View {
@State private var showingActionSheet = false
var body: some View {
NavigationView {
List {
HStack {
Text("Navigation Link")
// need to hide navigation link to use same chevrons
// because default one is different
NavigationLink(destination: Text("xx")) { EmptyView() }
Image(systemName: "chevron.right")
.foregroundColor(Color.gray)
}
HStack {
Button("Action Sheet") {
self.showingActionSheet = true
}
.foregroundColor(.black)
Spacer()
Image(systemName: "chevron.right")
.foregroundColor(Color.gray)
}
}
.listStyle(GroupedListStyle())
.actionSheet(isPresented: $showingActionSheet) {
ActionSheet(title: Text("Title"), buttons: [
.default(Text("Do Something")) { },
.cancel()
])
}
}
}
}