【问题标题】:NSTextField Drawing on Top of Sub View子视图顶部的 NSTextField 绘图
【发布时间】:2014-03-20 08:00:38
【问题描述】:

我创建了辅助NSViewController 来创建进度指示器“弹出窗口”。原因是软件必须与某些硬件交互,并且某些功能需要设备几秒钟才能响应。因此,考虑到最终用户,我有一个NSViewController,它有一个NSView(黑色和半透明),然后是顶部的消息/进度条。这是使用addSubView 添加到窗口中的。

除了屏幕上有NSTextField 之外,一切都很好。弹出窗口显示,但 NSTextField 绘制在顶部。这是什么?

我用于绘制半透明的视图代码:

@implementation ConnectingView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

    // Drawing code here.
    CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
    CGContextSetRGBFillColor(context, 0.227,0.251,0.337,0.8);
    CGContextFillRect(context, NSRectToCGRect(dirtyRect));
}

@end

我用来显示进度视图的代码

-(void) showProgressWithMessage:(NSString *) message andIsIndet:(BOOL) indet
{
    connectingView = [[ConnectingViewController alloc] init];
    [self.view.window.contentView addSubview:connectingView.view];
    connectingView.view.frame = ((NSView*)self.view.window.contentView).bounds;
    [connectingView changeProgressLabel:message];
    if (indet)
        [connectingView makeProgressBar:NO];
}

有没有更好的方法来添加子视图或告诉NSTextFields我不希望它们被绘制在顶部?

谢谢!

【问题讨论】:

  • 您是否考虑过使用NSPopover 来显示此进度?它可能会产生更好的结果。
  • 你设置WantsLayer了吗?

标签: macos cocoa nsview nstextfield


【解决方案1】:

所以将 [self setWantsLayer] 设置为我的自定义 NSViews 有点工作,但是有很多重绘问题(白色边框和背景)。在某些情况下,NSPopover 可能会更好,但是我打算采用“锁定”方法,其中接口在完成(或超时)之前无法访问。

对我有用的是转到我的 NSView 实例,在 Interface Builder 中选择窗口,然后转到图层(在属性视图的最右侧)并在“核心动画层”下选择我的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2016-11-27
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多