NSTabView with background color

前端 未结 3 1289
小鲜肉
小鲜肉 2021-01-03 09:11

As discussed elsewhere, NSTabView does not have a setBackgroundColor method and subclassing NSTabView and using an drawRect to control it does no longer work - as it does no

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 09:36

    If your situation can tolerate some fragility, a very simple and quick approach is to subclass NSTabView and manually adjust the frame of the item subviews. This gives each item a seamless yellow background:

    - (void)drawRect:(NSRect)dirtyRect {
        static const NSRect offsetRect = (NSRect) { -2, -16, 4, 18 };
    
        NSRect rect = self.contentRect;
    
        rect.origin.x += offsetRect.origin.x;
        rect.origin.y += offsetRect.origin.y;
        rect.size.width += offsetRect.size.width;
        rect.size.height += offsetRect.size.height;
    
        [[NSColor yellowColor] set];
        NSRectFill(rect);
    
        [super drawRect:dirtyRect];
    }
    

    A future change in the metrics of NSTabView would obviously be a problem so proceed at your own risk!

提交回复
热议问题