如何使用SwiftUI移除列表上的突出显示?
List {
}.whatModifierToAddHere?
Run Code Online (Sandbox Code Playgroud)
在选择管理文档犯规说什么。
Nic*_*elo 29
List {
ListItem()
.listRowBackground(Color.clear)
}
Run Code Online (Sandbox Code Playgroud)
Jul*_*lon 18
简单回答你的问题。点击时不想突出显示的任何单元格只需添加此修饰符
.buttonStyle(PlainButtonStyle())
Run Code Online (Sandbox Code Playgroud)
因此修饰符不是针对整个 List 是针对内部的每个单元格
var body: some View{
List{
ForEach(self.getElementsForList()){ element in
ElementCell(element: element)
.buttonStyle(PlainButtonStyle())
}
}
}
Run Code Online (Sandbox Code Playgroud)
iAl*_*x11 14
我知道我来晚了,但希望这能解决您的问题。您需要使用 UIKit 修饰符来删除它。我建议你把它们放在SceneDelegate.swift.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = TabController()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
//MARK: Disable selection.
UITableView.appearance().allowsSelection = false
UITableViewCell.appearance().selectionStyle = .none
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这将禁用您的应用程序中的所有表视图选择。相反,如果您想禁用特定表视图上的选择,您可以在init().
struct SomeViewWithTableView: View {
init() {
//MARK: Disable selection.
UITableView.appearance().allowsSelection = false
UITableViewCell.appearance().selectionStyle = .none
}
var body: some View {
//your view code here
}
}
Run Code Online (Sandbox Code Playgroud)
根据 iOS 的不同,这需要不同的方法。
对于 iOS 14:
.onAppear {
UITableViewCell.appearance().selectionStyle = .none
}
Run Code Online (Sandbox Code Playgroud)
对于 iOS 15:
List {
ListItem()
.listRowBackground(Color.clear)
}
Run Code Online (Sandbox Code Playgroud)
我必须添加.listRowBackground(Color(UIColor.secondarySystemGroupedBackground))到我的行中。
这会保留默认的背景颜色,而不是使其变得清晰。
我想你应该看看在短短的一篇文章如何禁用叠加颜色内部按钮和NavigationLink图片来自@TwoStraws
只需将.buttonStyle(PlainButtonStyle())修饰符添加到中的项目List,您将拥有所需的内容。这也使Buttons在再次工作List,这是我遇到的另一个问题。
import Combine
import SwiftUI
struct YourItem: Identifiable {
let id = UUID()
let text: String
}
class YourDataSource: ObservableObject {
let willChange = PassthroughSubject<Void, Never>()
var items = [YourItem]()
init() {
items = [
YourItem(text: "Some text"),
YourItem(text: "Some other text")
]
}
}
struct YourItemView: View {
var item: YourItem
var body: some View {
VStack(alignment: .leading) {
Text(item.text)
HStack {
Button(action: {
print("Like")
}) {
Image(systemName: "heart.fill")
}
Button(action: {
print("Star")
}) {
Image(systemName: "star.fill")
}
}
}
.buttonStyle(PlainButtonStyle())
}
}
struct YourListView: View {
@ObservedObject var dataSource = YourDataSource()
var body: some View {
List(dataSource.items) { item in
YourItemView(item: item)
}
.navigationBarTitle("List example", displayMode: .inline)
.edgesIgnoringSafeArea(.bottom)
}
}
#if DEBUG
struct YourListView_Previews: PreviewProvider {
static var previews: some View {
YourListView()
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
如文章所述,它也适用于NavigationLink。希望对您有所帮助