0

I want to add a NSVisualEffectView inside my NSTextView, however when I add it, the text is below the NSVisualEffectView, so, how can i add the NSVisualEffectView below the text?

My code for OS X:

class myTextView: NSTextView {

    override func awakeFromNib() {
        let visualEffectView = NSVisualEffectView(frame: NSMakeRect(20, 20, 30, 18))
        visualEffectView.material = NSVisualEffectMaterial.Dark
        visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
        self.addSubview(visualEffectView)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Geek20
  • 673
  • 1
  • 9
  • 18

2 Answers2

0
self.sendSubviewToBack(visualEffectView)

But the visual effect needs to be applied to the parent container, not the text view itself, so if I'm understanding your code correctly you need to move this whole thing up to whatever the parent view is.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • **sendSubviewToBack** isn't available for NSTextView – Geek20 Aug 16 '15 at 01:02
  • Yes, I just realized that you're applying this to the NSTextView. You need the effect to be on the parent containing view, if you want it to apply behind the NSTextView. – Ewan Mellor Aug 16 '15 at 01:03
0

Maybe this will help you:

class MyTextView: NSTextView {

    lazy var visualEffectView: NSVisualEffectView = {
        let visualEffectView = NSVisualEffectView()
        visualEffectView.material = NSVisualEffectMaterial.Dark
        visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow

        return visualEffectView
    }()

    override func awakeFromNib() {
        super.awakeFromNib()

        self.drawsBackground = false
    }

    override func viewDidMoveToSuperview() {
        super.viewDidMoveToSuperview()

        if let containerView = self.superview?.superview?.superview {
            containerView.addSubview(self.visualEffectView, positioned: .Below, relativeTo: self)
        }
    }

    override func resizeSubviewsWithOldSize(oldSize: NSSize) {
        super.resizeSubviewsWithOldSize(oldSize)

        if let superview = self.superview?.superview {
            self.visualEffectView.frame = superview.frame
        }
    }

}

It's a text view in a scroll view and clip view so you have to get its superview.

Bannings
  • 10,376
  • 7
  • 44
  • 54
  • I used your code and it works! thanks. Do you know how draw the visualEffectView with rounded corner with your code? – Geek20 Aug 16 '15 at 06:06
  • Consider the link: http://stackoverflow.com/questions/26518520/how-to-make-a-smooth-rounded-volume-like-os-x-window-with-nsvisualeffectview. If that resolved your issue please up-vote and accept it :) – Bannings Aug 16 '15 at 06:51