How to remove highlight on tap of List with SwiftUI?

前端 未结 6 866
悲哀的现实
悲哀的现实 2020-12-30 01:19

How to remove highlight on tap of List with SwiftUI?

List {

}.whatModifierToAddHere?

The selection manager documentation doesnt say anythi

相关标签:
6条回答
  • 2020-12-30 01:21

    Simple answer to you question. Any cell that you don't want to highlight when tapped just add this modifier

    .buttonStyle(PlainButtonStyle())
    

    Therefore the modifier is not for the whole List is for each cell inside

    var body: some View{
        List{
            ForEach(self.getElementsForList()){ element in
                ElementCell(element: element)
                    .buttonStyle(PlainButtonStyle())
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 01:24

    I know I'm a bit late but hope this will solve your problem. You need to use UIKit modifiers to remove this. I recommend you to place them on 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()
    
            }
        }
    }
    

    EDIT: This will disable all table view selections in your app. If instead, you want to disable the selection on a specific table view, you can disable it inside init().

    struct SomeViewWithTableView: View {
    
        init() {
            //MARK: Disable selection.
            UITableView.appearance().allowsSelection = false
            UITableViewCell.appearance().selectionStyle = .none
        }
    
        var body: some View {
            //your view code here
        }
    }
    
    0 讨论(0)
  • 2020-12-30 01:36

    A possible solution is to use a ZStack. And adjust insets to .zero. Change Color.red to your color. Your List:

    ForEach(items) { item in
        ZStack {
            NavigationLink(destination: DetailView()) {
                EmptyView()
            }
            ItemRowWrap(item: item)
                .background(Color.red)
        }
        
    }
    .listStyle(InsetListStyle())
    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
    

    Your ItemRowView:

    struct ItemRowWrap: View {
        
        let item: ListItem
        
        var body: some View {
            ItemRow(item: item)
                .padding(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
        }
        
    }
    

    You can adjust paddings as you need.

    0 讨论(0)
  • 2020-12-30 01:39

    Edit:

    The following only works if the SwiftUI view is embedded in a UIKit parent.



    For anyone still wondering, you can use .onAppear modifier to achieve this.

    .onAppear {
                UITableViewCell.appearance().selectionStyle = .none
        }
    

    This is similar to viewWillAppear in UIKit. Source: HackingWithSwift

    0 讨论(0)
  • 2020-12-30 01:41

    Just adding the .buttonStyle(PlainButtonStyle()) modifier to the item in the List, unfortunately didn't work for my case. Instead I was able to obtain the desired effect -- not seeing any highlighting effect when tapping on a row -- by using the .onAppear() modifier on the List and the Appearance API for UITableViewCell.

    As in the following example:

    List {
        ForEach(items) { item in
            NavigationLink(destination: DetailView(item)) {
                RowView(item)
            }
        }
    }
    .onAppear {
    
        // this will disable highlighting the cell when is selected
        UITableViewCell.appearance().selectionStyle = .none
    
        // you can also remove the row separators
        UITableView.appearance().separatorStyle = .none
    
    }
    
    0 讨论(0)
  • 2020-12-30 01:48

    I know I'm a bit late, but it's for those of you who are searching (like me

    0 讨论(0)
提交回复
热议问题