How to remove highlight on tap of List with SwiftUI?
List {
}.whatModifierToAddHere?
The selection manager documentation doesnt say anythi
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())
}
}
}
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
}
}
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.
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
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
}