NSTabView Background Color

1.3k views Asked by At

I have a window controller with blue background color

The background color of all children view controllers work fine with blue background just like their parent window controller, except NSTabView of NSTabViewController, which draws with the original gray background

I am not sure why it does not inherit the background color, or why it is not transparent

Anyway, to solve the issue, I tried the following

(1)

I tried editing the CLayer background

tabView.layer?.backgroundColor = NSColor.blue.cgColor

I tried putting that code everywhere, and the only time it worked is when I accessed the CLayer from a tabViewItem inside its viewWillAppear function or viewDidAppear or viewWillLayout, but the problem is that this code does not work until I select another tab... i.e. when the tabViewController is shown at the beginning, the initial view background color is grey, and does not change to blue until I select another tab!

From my understanding, the tabView is drawn after its child viewDidAppear, that's why it overcame the blue color order, and when another tab is selected, the viewDidAppear function is called again changing the color to blue (since the tabView` is already drawn)

(2)

I tried to change drawsBackground value to false

when the tabviewcontroller is shown, the background is just like what its windows is, blue, but as soon as I choose another tab, black residue are left from the previous tabviewitem controls

Black residue from the previous tab controls

any idea how to solve this problem is much appreciated!

2

There are 2 answers

0
Anya Alstreim On

I solved this by adding a Custom Box to the initial tab with the required background color, and resized it to the size of the view

In the rest of tab views, I added this line to viewWillAppear function

tabView.layer?.backgroundColor = NSColor.blue.cgColor

The custom box feels like a hack but is the only way around I could come up with

0
Ely On

An alternative solution is to place a custom NSBox behind the NSTabView with the correct size and background color. No further code needed, as long as all tabs feature a transparent background.