简体   繁体   English

将NSView添加到窗口不在顶部?

[英]Adding NSView to window not on top?

I am adding a subview programmatically and adding it to the main windows context view to cover up the entire context view like so: 我正在以编程方式添加一个子视图,并将其添加到主Windows上下文视图中,以掩盖整个上下文视图,如下所示:

loadingView = [[LoadingView alloc] initWithFrame:[mainWindow.contentView frame]];

NSLog(@"%@", [mainWindow.contentView subviews]);
[mainWindow.contentView addSubview:loadingView];
NSLog(@"%@", [mainWindow.contentView subviews]);

[mainWindow makeFirstResponder:loadingView];

The NSLog's confirm that loadingView is being added last in the contentView subviews. NSLog's确认loadingView是在contentView子视图中最后添加的。 I have also tried: 我也尝试过:

loadingView = [[LoadingView alloc] initWithFrame:[mainWindow.contentView frame]];
[mainWindow.contentView addSubview:loadingView positioned:NSWindowAbove relativeTo:nil];
[mainWindow makeFirstResponder:loadingView];

That didn't work either. 那也不起作用。 For some reason the two tableviews (created in IB) at the bottom of the window are on top of the new view I've added. 出于某种原因,窗口底部的两个表视图(在IB中创建)位于我添加的新视图的顶部。 Here's a snapshot of the window, note that the red part is what should be on top with the progress bar and a few labels: 这是窗口的快照,请注意红色部分是进度条和一些标签应位于顶部的位置:

loadingView

It's also worth noting that the view has it's alpha set to 0.9 which is why you can somewhat see behind it. 还值得注意的是,该视图的Alpha值设置为0.9,这就是为什么您可以在其后面有所看到的原因。

GW GW

If you place one view above another, the objects in the previous view will be visible in above view. 如果将一个视图放在另一个视图上方,则上一个视图中的对象将可见。 What you need do is remove previous views from window and then add a new subview. 您需要做的是从窗口中删除先前的视图,然后添加一个新的子视图。

Try using: 尝试使用:

//Create IBOutlet of your tableview in your .h file
IBOutlet NSTableView* yourTableView;

// Add this line where you are adding your subview to remove the tableview from superview.
[yourTableView removeFromSuperview];

// Then add your loading view as the subview
loadingView = [[LoadingView alloc] initWithFrame:[mainWindow.contentView frame]];
[mainWindow.contentView addSubview:loadingView];

Then whenever your want your tableView back use: 然后,每当您希望您的tableView返回使用时:

[window contentView]addSubview: yourTableView];

As long as you use nil here, you will not get predictable results. 只要您在此处使用nil,就不会得到可预测的结果。

[mainWindow.contentView addSubview:loadingView positioned:NSWindowAbove relativeTo:nil]; [mainWindow.contentView addSubview:loadingView位置:NSWindowAbove relativeTo:nil];

If you have not already done so, put all the other views inside a containing view. 如果尚未这样做,请将所有其他视图放入一个包含视图。 Make the containing view the only view that is a direct child of the window content view. 将包含视图设为窗口内容视图的直接子视图。 Now add your subview with the above method and replace nil with a reference to the containing view. 现在,使用上述方法添加子视图,并将nil替换为对包含视图的引用。

In OS X, overlapping siblings have certain nuances when it comes to drawing. 在OS X中,重叠的同级元素在绘制时会有一些细微差别。 In your case, the loadingView and the two table views are siblings because they are all added as subviews of the window's content view and they overlap hence the nuances are coming into play. 在您的情况下,loadingView和两个表视图是同级的,因为它们都被添加为窗口内容视图的子视图,并且它们重叠,因此细微差别开始发挥作用。

From Apple's Documentation 从苹果的文档

For performance reasons, Cocoa does not enforce clipping among sibling views or guarantee correct invalidation and drawing behavior when sibling views overlap. 出于性能方面的考虑,当同级视图重叠时,Cocoa不会在同级视图之间强制剪切或保证正确的无效和绘制行为。 If you want a view to be drawn in front of another view, you should make the front view a subview (or descendant) of the rear view. 如果要在另一个视图前面绘制一个视图,则应将前视图作为后视图的子视图(或后代)。

I don't have the definitive solution for this but reading these should help improve your understanding for the long term. 我没有确定的解决方案,但是长期阅读这些内容将有助于提高您的理解能力。

http://www.cocoabuilder.com/archive/cocoa/327817-overlapping-sibling-views.html http://www.cocoabuilder.com/archive/cocoa/327817-overlapping-sibling-views.html

Is there a proper way to handle overlapping NSView siblings? 有正确的方法来处理重叠的NSView兄弟姐妹吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM