【问题标题】:NSWindow Transparent Background NSCollectionView background not repaintingNSWindow 透明背景 NSCollectionView 背景不重绘
【发布时间】:2020-08-17 08:06:29
【问题描述】:

我正在尝试创建一个半透明的 macOS 应用程序。就像 macOS 中包含的终端应用程序一样。我在 Storyboard 中创建了一个透明的 NSWindow 子类和一个非常标准的 NSCollectionView。我可以让应用程序是半透明的,但是当我滚动 NSCollectionViewItems 时,它会使背景完全透明。

问题的代码在这里Github

问题示例

我尝试了很多东西

  • 获取NSScroll View的回调,获取UI重绘
  • 在 Storyboard 的大多数视图中设置背景
  • StackOverflow 问题的建议:StackOverflow

这是自定义 UIWindow

import Cocoa

class MainWindow: NSWindow {

    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: false)
        self.backingType = NSWindow.BackingStoreType.buffered
        self.alphaValue = 1.0
        self.backgroundColor = NSColor.clear
        self.isOpaque = false
    }
}

这里是视图控制器

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var collectionView: NSCollectionView!
    @IBOutlet weak var scrollView: NSScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        configureCollectionView()
        collectionView.register(ReceiverCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"))
    }

    private func configureCollectionView() {
        let flowLayout = NSCollectionViewFlowLayout()
        flowLayout.itemSize = NSSize(width: 100.0, height: 100.0)
        flowLayout.sectionInset = NSEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
        flowLayout.minimumInteritemSpacing = 20.0
        flowLayout.minimumLineSpacing = 20.0
        collectionView.collectionViewLayout = flowLayout

        scrollView.drawsBackground = false
        scrollView.contentView.drawsBackground = false
        scrollView.backgroundColor = NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.1)

        collectionView.backgroundColors = [NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.1)]
    }
}

extension ViewController: NSCollectionViewDataSource {
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), for: indexPath) 
        return item
    }
}

【问题讨论】:

    标签: swift xcode macos nscollectionview


    【解决方案1】:

    经过多次反复试验(更多错误),我找到了解决方案。

    在 NSWindow 子类中,如果您设置背景颜色,则会消除背景中清晰的“洞”。

    import Cocoa
    
    class MainWindow: NSWindow {
    
        override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
            super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: false)
            self.backingType = NSWindow.BackingStoreType.buffered
            self.alphaValue = 1.0
            self.backgroundColor = NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)
            self.isOpaque = false
        }
    }
    

    ViewController 还需要一些东西

    import Cocoa
    
    class ViewController: NSViewController {
    
        @IBOutlet weak var collectionView: NSCollectionView!
        @IBOutlet weak var scrollView: NSScrollView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            configureCollectionView()
            collectionView.register(ReceiverCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"))
        }
    
        private func configureCollectionView() {
            let flowLayout = NSCollectionViewFlowLayout()
            flowLayout.itemSize = NSSize(width: 100.0, height: 100.0)
            flowLayout.sectionInset = NSEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
            flowLayout.minimumInteritemSpacing = 20.0
            flowLayout.minimumLineSpacing = 20.0
            collectionView.collectionViewLayout = flowLayout
    
            scrollView.drawsBackground = false
            scrollView.contentView.drawsBackground = false
            scrollView.backgroundColor = NSColor.clear
    
            collectionView.backgroundColors = [.clear]
        }
    }
    
    extension ViewController: NSCollectionViewDataSource {
        func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
            let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), for: indexPath) 
            return item
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-22
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2010-09-08
      • 1970-01-01
      相关资源
      最近更新 更多