如何使用SwiftUI移除列表上的突出显示?

iOS*_*com 8 ios swiftui

如何使用SwiftUI移除列表上的突出显示?

List {

}.whatModifierToAddHere?
Run Code Online (Sandbox Code Playgroud)

选择管理文档犯规说什么。

Nic*_*elo 29

.listRowBackground对我有用

List {
   ListItem()
       .listRowBackground(Color.clear)
}
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,只需使用“listRowBackground”,所选的灰色背景就会消失。 (5认同)
  • 这是在 iOS 16.1 上对我有用的唯一答案。我尝试了所有其他解决方案,但没有任何效果,谢谢!请注意,我正在做 iOS/macOS,所以我的实际解决方案如下: ``` #if os(iOS) .listRowBackground(Color.white) #elseif os(macOS) .listRowBackground(Color.clear) #endif ``` (2认同)

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)

  • 抱歉,不适用于 iOS 16。 (2认同)

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)


Vkh*_*arb 6

根据 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)


Mar*_*ens 6

iOS 17

我必须添加.listRowBackground(Color(UIColor.secondarySystemGroupedBackground))到我的行中。

这会保留默认的背景颜色,而不是使其变得清晰。


Rém*_* B. 5

我知道我来晚了,但这是针对那些正在搜寻的人(例如我)

我发现了什么

我想你应该看看在短短的一篇文章如何禁用叠加颜色内部按钮和NavigationLink图片来自@TwoStraws

只需将.buttonStyle(PlainButtonStyle())修饰符添加到中的项目List,您将拥有所需的内容。这也使Buttons在再次工作List,这是我遇到的另一个问题。

一个Swift 5.1的工作示例:

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。希望对您有所帮助

  • 此解决方案在 XCode 11.3.1 上不起作用,点击列表项时仍然会出现选择,我使用的项目是 ZStack (6认同)