2016-08-17 68 views
0

我在故事板中有两个NSTextField s,两者都保持未改变默认配置,除非我已将其类更改为子类。我也划分了内部NSTextFieldCell。我已经根据需要定制了每个 - 非常简单的子类。第一个响应者的文本字段按预期显示,但其他文本字段不显示。它为它的细胞绘制了一个白色背景。在两个字段之间切换切换背景。我怎样才能从单元格中移除这个白色背景?NSTextFieldCell仅在不是第一响应者时绘制白色背景

LoginTextField

- (void)baseInit { 
    self.drawsBackground = NO; 
    self.wantsLayer = YES; 
    self.backgroundColor = [[NSColor whiteColor] colorWithAlphaComponent:0.75]; 
    self.textColor = [NSColor blackColor]; 

    CALayer *layer = [[CALayer alloc] init]; 
    layer.backgroundColor = self.backgroundColor.CGColor; 
    layer.borderWidth = 0; 
    layer.cornerRadius = 6; 

    self.layer = layer; 
} 

LoginTextFieldCell

- (void)baseInit { 
    self.drawsBackground = NO; 
    self.focusRingType = NSFocusRingTypeNone; 
} 

enter image description here

如果我重写此功能,白色背景消失,但这样做的占位符和文本标签时不是第一次响应者。

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    return [super drawInteriorWithFrame:cellFrame inView:controlView]; 
} 

enter image description here

我开发在Xcode 7的应用程序,在OS X埃尔卡皮坦运行。

回答

0

对于任何人谁也看到了这个问题,这对我工作的解决方案是设置borderedNO和配置self.layer,而不是创建一个新的CALayer

LoginTextField

- (void)baseInit { 
    self.drawsBackground = NO; 
    self.wantsLayer = YES; 
    self.textColor = [NSColor blackColor]; 
    self.bordered = NO; 

    self.layer.backgroundColor = [[NSColor whiteColor] colorWithAlphaComponent:0.75].CGColor; 
    self.layer.borderWidth = 0; 
    self.layer.cornerRadius = 6; 
}