Search code examples
objective-cmacoscocoanstextfieldnspopover

NSPopover with a NSTextField shows the text field with an opaque background as it's opening, and then goes transparent after the popover animation


There's actually a little different behavior across OSs
This is what the popover looks like as it's animating open in 10.10:
enter image description here
If you look closely, you can see a little opaque border around the NSTextField

Here is what it looks like in 10.11:
enter image description here There's just a straight up opaque background

And here's what the popover looks like after the animation, fully shown:
enter image description here

Modifying the popover's appearance does not fix the issue:
enter image description here

What we have here is an NSViewController, which just has a container NSView, and a NSTextField. The container is so that we can add padding through auto layout constraints

@interface MessageViewController ()
@property (strong) IBOutlet NSTextField *messageLabel;
@property (weak) IBOutlet NSLayoutConstraint *rightPadding;
@property (weak) IBOutlet NSLayoutConstraint *topPadding;
@property (weak) IBOutlet NSLayoutConstraint *bottomPadding;
@property (weak) IBOutlet NSLayoutConstraint *leftPadding;
@end

@implementation MessageViewController

@synthesize message = _message;

- (instancetype)initWithMessage:(NSString *)message andPadding:(CGFloat)padding
{
   self = [super init];
   if( self )
   {
      [self loadView];
      self.rightPadding.constant = padding;
      self.topPadding.constant = padding;
      self.bottomPadding.constant = padding;
      self.leftPadding.constant = padding;
      self.message = message;
   }
   return self;
}

- (void)setMessage:(NSString *)message
{
   _message = message;
   self.messageLabel.stringValue = message;
}

- (NSString *)message
{
   return _message;
}

That's why you can see the border, and only a portion of the popover is opaque. It is that inner NSTextField that has the background/border, and the container view keeps the transparent background. It isn't until the popover is fully shown that the background/border goes transparent as well

Also here is the xib:
enter image description here

How could this be?

I have tried setting the background color property, and have tried setting the properties in -awakeFromNib instead of in the init, to no avail


Solution

  • Resolved the issue, in interface builder had to check the view and label in the "View Effects Inspector", which sets wantsLayer = YES

    Probably could be done programmatically too