如何设置iPhone UIView z索引?

发布于 2024-10-11 13:58:01 字数 48 浏览 9 评论 0 原文

我想将一个视图移动到另一个视图之上,如何知道视图的 z 索引,以及如何移动到顶部?

I want to move one view on top of another, how can I know the z index of the view, and how to move on to top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

躲猫猫 2024-10-18 13:58:02

您可以使用视图图层的 zPosition 属性(它是一个 CALayer 对象)来更改视图的 z-index。

theView.layer.zPosition = 1;

Viktor Nordling 补充道,“大值位于顶部。您可以使用任何您想要的值,包括负值。 ”默认值为0。

需要导入QuartzCore框架才能访问该层。只需将这行代码添加到实现文件的顶部即可。

#import "QuartzCore/QuartzCore.h"

You can use the zPosition property of the view's layer (it's a CALayer object) to change the z-index of the view.

theView.layer.zPosition = 1;

As Viktor Nordling added, "big values are on top. You can use any values you want, including negative values." The default value is 0.

You need to import the QuartzCore framework to access the layer. Just add this line of code at the top of your implementation file.

#import "QuartzCore/QuartzCore.h"
三生殊途 2024-10-18 13:58:02

UIView 同级按照添加到其超级视图的顺序堆叠。 UIView 层次结构方法和属性用于管理视图顺序。在 UIView.h 中:

@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;

- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;

同级视图在 subviews 数组中从后到前排序。所以最上面的视图将是:

[parentView.subviews lastObject];

底部视图将是:

[parentView.subviews objectAtIndex:0];

就像 Kolin Krewinkel 所说,[parentView BringSubviewToFront:view] 会将视图带到顶部,但只有当视图都是时才会出现这种情况等级制度中的兄弟姐妹。

UIView siblings are stacked in the order in which they are added to their superview. The UIView hierarchy methods and properties are there to manage view order. In UIView.h:

@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;

- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;

The sibling views are ordered back to front in the subviews array. So the topmost view will be:

[parentView.subviews lastObject];

and bottom view will be:

[parentView.subviews objectAtIndex:0];

Like Kolin Krewinkel said, [parentView bringSubviewToFront:view] will bring the view to the top, but this is only the case if the views are all siblings in the hierarchy.

初懵 2024-10-18 13:58:02

IB 和 Swift

鉴于流动布局,其中黄色是超级视图,红色、绿色和蓝色是黄色的同级子视图,

views - 顶部的红色视图

目标是移动子视图(让我们说绿色)到顶部。

视图 - 绿色顶部视图

在 Interface Builder 中

在 Interface Builder 中,您所需要做的就是将要在顶部显示的视图拖到文档大纲中列表的底部。

或者,您可以选择视图,然后在菜单中转到“编辑器 安排>发送到前面。

在 Swift 中,

有几种不同的方法可以通过编程来完成此操作。

方法 1

yellowView.bringSubviewToFront(greenView)
  • 此方法与上面的 IB 答案在编程上等效。

  • 仅当子视图互为同级时才有效。

  • 子视图数组包含在yellowView.subviews中。此处,bringSubviewToFrontgreenView 从索引 0 移动到 2。这可以通过观察

     print(yellowView.subviews.indexOf(greenView))
    

方法 2

greenView.layer.zPosition = 1
  • 观察到。该方法只是将图层的 3D 位置在 z 轴上移得更高(更靠近用户)。由于所有其他视图的默认值为 0,因此结果是 greenView 看起来位于顶部。但是,它仍然保留在 yellowView.subviews 数组的索引 0 处。不过,这可能会导致一些意外的结果,因为点击事件之类的事件仍然会首先到达索引号最高的视图。因此,最好采用上面的方法 1。
  • zPosition 可以设置为 CGFloat.greatestFiniteMagnitude(旧版本 Swift 中的 CGFloat(FLT_MAX))以确保它位于顶部。

IB and Swift

Given the flowing layout where yellow is the superview and red, green, and blue are sibling subviews of yellow,

views - red view on top

the goal is to move a subview (let's say green) to the top.

views - green view on top

In Interface Builder

In the Interface Builder all you need to do is drag the view you want showing on the top to the bottom of the list in the Documents Outline.

order of views in Interface Builder

Alternatively, you can select the view and then in the menu go to Editor > Arrange > Send to Front.

In Swift

There are a couple of different ways to do this programmatically.

Method 1

yellowView.bringSubviewToFront(greenView)
  • This method is the programmatic equivalent of the IB answer above.

  • It only works if the subviews are siblings of each other.

  • An array of the subviews is contained in yellowView.subviews. Here, bringSubviewToFront moves the greenView from index 0 to 2. This can be observed with

      print(yellowView.subviews.indexOf(greenView))
    

Method 2

greenView.layer.zPosition = 1
  • This method just moves the 3D position of the layer higher (closer to the user) on the z-axis. Since the default is 0 for all the other views, the result is that the greenView looks like it is on top. However, it still remains at index 0 of the yellowView.subviews array. This can cause some unexpected results, though, because things like tap events will still go first to the view with the highest index number. For that reason, it might be better to go with Method 1 above.
  • The zPosition could be set to CGFloat.greatestFiniteMagnitude (CGFloat(FLT_MAX) in older versions of Swift) to ensure that it is on top.
少钕鈤記 2024-10-18 13:58:02
[parentView bringSubviewToFront:view] ;
[parentView bringSubviewToFront:view] ;
夜巴黎 2024-10-18 13:58:02

如果您想通过 XCode 的 Interface Builder 来完成此操作,可以使用 Editor->Arrangement 下的菜单选项。在那里你会找到“发送到前面”、“发送到后面”等。

If you want to do this through XCode's Interface Builder, you can use the menu options under Editor->Arrangement. There you'll find "Send to Front", "Send to Back", etc.

朦胧时间 2024-10-18 13:58:02

在您想要到达顶部的视图中......
(快速)

superview?.bringSubviewToFront(self)

Within the view you want to bring to the top...
(in swift)

superview?.bringSubviewToFront(self)
浮萍、无处依 2024-10-18 13:58:02

我们可以在 ios 中使用 zPosition

如果我们有一个名为 salonDetailView 的视图,
例如: @IBOutlet weak var salalDetailView: UIView!

就我而言,请参阅图像

并拥有 GMSMapView 的 UIView
例如:@IBOutlet weak var mapViewUI: GMSMapView!

要显示mapViewUI上方的视图salonDetailView

使用zPosition如下

salonDetailView.layer.zPosition = 1

We can use zPosition in ios

if we have a view named salonDetailView
eg : @IBOutlet weak var salonDetailView: UIView!

In my case see the image

and have UIView for GMSMapView
eg : @IBOutlet weak var mapViewUI: GMSMapView!

To show the View salonDetailView upper of the mapViewUI

use zPosition as below

salonDetailView.layer.zPosition = 1
旧情勿念 2024-10-18 13:58:02

如果您使用的是 cocos2d,您可能会看到 [parentView BringSubviewToFront:view] 的问题,至少它对我不起作用。我没有将我想要的视图放在前面,而是将其他视图发回,这就达到了目的。

[[[CCDirector sharedDirector] view] sendSubviewToBack:((UIButton *) button)]; 

If you are using cocos2d, you may see an issue with [parentView bringSubviewToFront:view], at least it was not working for me. Instead of bringing the view I wanted to the front, I send the other views back and that did the trick.

[[[CCDirector sharedDirector] view] sendSubviewToBack:((UIButton *) button)]; 
柠檬 2024-10-18 13:58:02

使用它

- (void)insertSubview:(UIView *)view belowSubview:(UIView*)siblingSubview;

或者按照此处

Or use this

- (void)insertSubview:(UIView *)view belowSubview:(UIView*)siblingSubview;

as per here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文