CALayer issues while changing NSView background

AnaghSharmaAnaghSharma USMember ✭✭

I am trying to add a subview to a view by calling the AddSubview method on view. Since I want the View's and Subviews' background to be of custom colour, I customised their appearance by setting WantsLayer to true and changing the Layer.BackgroundColor property. It works fine - the subview gets added to the view with the desired background. But I am having strange issues as I am seeing blocks around the elements -

As you can see, I can see blocks of black colour around Progress Indicator and Loading label. I can even see a block of View's label (top left) - that is actually behind the subview. I guess the issue is because of the layer enabling.

P.S.: I know this is not a Xamarin.Mac specific question, but I have searched a lot about this issue and still not able to find anything. Posting here hoping that I would get to know what the issue could be. The app is a macOS menu bar app. The subview is loaded from a .xib file.

Tagged:

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    edited March 2018

    So it's a bit difficult to answer your specific question without more details / a sample at very least, but a few things come to mind.

    • Some Cocoa controls do not paint correctly when parented in Layer backed (or hosted views). Maybe the controls in particular are having issues.
    • Some Cocoa controls don't paint as you wish when you monkey with their layers\backgrounds.

    Unfortunately, Cocoa is not as extendable as other GUI toolkits, and changing the background of controls may involve more than just setting the background on the relevant layer.

    A few things I would try:

    • Separate the layer hosting from background change. First set WantsLayer on your root container and verify the drawing of the relevant controls.
    • If that is successful, you could try setting the background of smaller subsets of controls to determine what exact subview is not doing what you want.
    • It should be trivial to create an Obj-c app reproducing your drawing with background issue without C#.
    • You could report that to Apple, but likely you won't get anywhere with that.
    • You may have to subclass or create custom controls for your use here. :(
  • AnaghSharmaAnaghSharma USMember ✭✭

    @ChrisHamons I'll try to provide some details - I have a .xib file which has a view containing a label and Progress Indicator. I have the corresponding .cs file for the view in which I have set the background colour of the view as -

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();
    
        WantsLayer = true;
        Layer.BackgroundColor = new CoreGraphics.CGColor(0.07f, 0.07f, 0.07f, 1.0f);
    }
    

    I am adding this view to the View (let's call it superview - whose background is also custom like above) of main ViewController.
    The superview does not contain any element except a label (which apparently I can't remove). The subview gets added on top of the superview and I get the result as you can see in the image I posted earlier.

    And for the things you noted out -
    • Separating the layer hosting from background change - how this can be done? Of all the things I have searched changing the background using CALayer is the best way. I can't put an NSBox in the background as it does not cover the triangle part of popover.
    • Setting the background of every control I am going to use seems like an overkill.

    I don't have much idea of Layer backed views, so please guide me accordingly. Thank you.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Yeah, you are setting the parent view's to wants layers and set the background color, and some child view isn't doing the right thing, as suspected.

    You could try commenting out the background color for a moment, and see if things are drawing correctly with just the layer enabled (Is the problem with the child views the background or the layer enabled?)

    Beyond that, as I mentioned, some build in Cocoa controls do not handle having layers with background color set correctly. If things are not drawing, you may have to subclass them and "do the right thing" in drawing yourself. This can unfortunately be a bit painful, as Cocoa is not as extendable as other toolkits. Apple really doesn't want you monkeying around with default controls's appearances, you "should" be either making your own custom views or keep stock things looking stock (in their opinion from what I can tell).

Sign In or Register to comment.