列表Mac OS的SwiftUI背景颜色 [英] SwiftUI background color of List Mac OS

查看:210
本文介绍了列表Mac OS的SwiftUI背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS中使用ListView.我试图更改该ListView的背景颜色.但是,这并不像预期的那么容易.

I am using a ListView in Mac OS. I am trying to change that background color of that ListView. However, it is not that easy as expected.

我尝试在ListView上使用.background(Color(.red))属性.那并没有改变任何东西.

I tried using .background(Color(.red)) attribute on the ListView. That didn't change anything.

我只能找到对表行有影响的.listRowBackground(Color(.red)).但是,其他背景没有受到影响.

I could only find .listRowBackground(Color(.red))which had an influence on the table rows. However, the other background wasn't effected.

我准备了一个小演示来演示:

I prepared a little demo to demonstrate:

在我看来:

  VStack
    {
        List()
        {
            Text("Test")
                .listRowBackground(Color.green)

            Text("Test")
                .listRowBackground(Color.green)

            Text("Test")
                .listRowBackground(Color.green)

        }.background(Color(.red))

    }.background(Color(.red))

那是我得到的结果:

主要背景不变.我读到有关更改UITableView.appearance的解决方案的信息,但是在Mac OS的SwiftUI中,这对我来说是不可能的.

The main background does not change. I read about a solution changing the UITableView.appearance but that is not possible for me in SwiftUI for Mac OS.

预先感谢

推荐答案

第一张图片显示了问题的根源-默认使用的滚动视图不透明,因此添加了彩色背景但不可见

First picture shows the origin of the issue - used scroll view is opaque by default, so added color background is there but not visible

因此解决方案是找到该滚动视图并禁用绘图背景

So the solution is to find that scroll view and disable drawing background

这里是解决此问题或解决方法的可行方法(但由于所有修改都是通过AppKit API进行的,因此没有硬编码,因此是有效的).实际上,这与关闭XIB中用于持有我们的tableview的scrollview的复选框相同.在Xcode 11.2/macOS 10.15上进行了测试.

Here is possible approach to solve this, or workaround (but valid as all modifications are made via AppKit API, no hardcoding). Actually it is the same as it would turned off checkbox in XIB for scrollview holding our tableview. Tested with Xcode 11.2 / macOS 10.15.

struct ScrollViewCleaner: NSViewRepresentable {

    func makeNSView(context: NSViewRepresentableContext<ScrollViewCleaner>) -> NSView {
        let nsView = NSView()
        DispatchQueue.main.async { // on next event nsView will be in view hierarchy
            if let scrollView = nsView.enclosingScrollView {
                scrollView.drawsBackground = false
            }
        }
        return nsView
    }

    func updateNSView(_ nsView: NSView, context: NSViewRepresentableContext<ScrollViewCleaner>) {
    }
}

extension View {
    func removingScrollViewBackground() -> some View {
        self.background(ScrollViewCleaner())
    }
}

struct TestListBackground: View {
    var body: some View {
            List()
            {
                ForEach(0..<3) { _ in
                    Text("Test")
                        .listRowBackground(Color.green)
                }
                .removingScrollViewBackground() // must be called _inside_ List
                .background(Color.blue)
            }.background(Color.red)
    }
}

这篇关于列表Mac OS的SwiftUI背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆