简体   繁体   English

如何使NSButton在NSButton之上?

[英]How to make the NSButton above a NSButton?

i'm having two NSButtons. 我有两个NSButton。 Both button were with images. 两个按钮都带有图像。 This is what i created [buttons] ( http://i.stack.imgur.com/IYXIE.png ) the problem is the star button always located behind the leaf button. 这就是我创建的[buttons]( http://i.stack.imgur.com/IYXIE.png )问题是星型按钮始终位于叶子按钮的后面。 how can i bring it front. 我怎么能把它放在前面。 (searching for Mac osx not iOS.) (搜索Mac osx而不是iOS。)
UPDATE: this is what i got during first mousehover 更新:这是我第一次鼠标悬停时得到的 在此处输入图片说明 , got this during second mouse over ,在第二次滑过鼠标时得到了 在此处输入图片说明

[starButton removeFromSuperview];
[self addSubview:starButton positioned:NSWindowAbove relativeTo:nil];

Also, NSView supports the method 另外,NSView支持该方法

 sortSubviewsUsingFunction:context:

implement that to simply sort the views.. requires more code but seems more clean. 实现只对视图排序的方法..需要更多代码,但看起来更干净。

You can use bringSubviewToFront to force a view appear "on top" of another view: 您可以使用BringSubviewToFront强制一个视图出现在另一个视图的“顶部”:

[parentView bringSubviewToFront:view];

If you want to change the order of the views in the storyboard designer, just drag the view you want on top below the other view in the left menu. 如果要在情节提要设计器中更改视图的顺序,只需将所需的视图拖到左侧菜单中另一个视图下方的顶部即可。 The topmost view is always the one displayed last in the list. 最顶部的视图始终是列表中最后显示的视图。

故事板

Instead of star I am using A , and Apple instead of Leave. 我使用A代替星号,使用Apple代替Leave。

You are doing this way: 您正在这样做: 在此处输入图片说明

You need to change the buttons location as : 您需要将按钮位置更改为:

在此处输入图片说明

Bring button A after Apple . 将按钮A放在Apple后面

then you will see as per your requirement . 然后您将根据您的要求进行查看。

在此处输入图片说明

This will do 这会做

[parentView bringSubviewToFront:leafButton];
[parentView bringSubviewToFront:starButton];

To bring a subview to the front on macOS, do this: 要将子视图置于macOS的最前面,请执行以下操作:

NSView *superView = myObject.superview;
[myObject removeFromSuperview];
[superView addSubview:myObject]; // brings to the front

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

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