ios 如何设置 iPhone UIView z 索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4631878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:28:20  来源:igfitidea点击:

How to set iPhone UIView z index?

iosuiviewpositionz-index

提问by Tattat

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?

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

回答by Daniel says Reinstate Monica

You can use the zPositionproperty of the view's layer (it's a CALayerobject) to change the z-index of the view.

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

theView.layer.zPosition = 1;

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

正如Viktor Nordling 所补充的那样,“大值在上。你可以使用任何你想要的值,包括负值。” 默认值为 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.

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

#import "QuartzCore/QuartzCore.h"

回答by Nathan Eror

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

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;

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

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

[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.

就像 Kolin Krewinkel 所说的那样,[parentView bringSubviewToFront:view]将视图带到顶部,但只有当视图在层次结构中都是兄弟时才会出现这种情况。

回答by Suragch

IB and Swift

IB 和 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

Interface Builder 中的视图顺序

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

或者,您可以选择视图,然后在菜单中转到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, bringSubviewToFrontmoves the greenViewfrom index 0to 2. This can be observed with

    print(yellowView.subviews.indexOf(greenView))
    
  • 此方法是上述 IB 答案的编程等效方法。
  • 仅当子视图是彼此的兄弟时才有效。
  • 子视图的数组包含在yellowView.subviews. 在这里,bringSubviewToFrontgreenViewfrom 索引移动02. 这可以观察到

    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 0for all the other views, the result is that the greenViewlooks like it is on top. However, it still remains at index 0of the yellowView.subviewsarray. 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 zPositioncould be set to CGFloat.greatestFiniteMagnitude(CGFloat(FLT_MAX)in older versions of Swift) to ensure that it is on top.
  • 此方法只是将图层的 3D 位置在 z 轴上移得更高(更靠近用户)。由于默认值0适用于所有其他视图,因此结果greenView看起来像是在顶部。但是,它仍然保留在数组的索引0yellowView.subviews。但是,这可能会导致一些意想不到的结果,因为诸如点击事件之类的事情仍将首先进入具有最高索引号的视图。因此,最好采用上面的方法 1。
  • zPosition可以设置为CGFloat.greatestFiniteMagnitudeCGFloat(FLT_MAX)在旧版本的雨燕),以确保其在上面。

回答by Kolin Krewinkel

[parentView bringSubviewToFront:view] ;

回答by larspars

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.

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

回答by Carter Medlin

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

在您想要带到顶部的视图中......(迅速)

superview?.bringSubviewToFront(self)

回答by Madhav Sbss

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.

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

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

回答by abhijith k

We can use zPosition in ios

我们可以在ios中使用zPosition

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

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

In my case see the image

就我而言,请参见图片

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

并为GMSMapView提供 UIView 例如:@IBOutlet weak var mapViewUI: GMSMapView!

To show the View salonDetailViewupper of the mapViewUI

显示mapViewUI上方的ViewsalonDetailView

use zPosition as below

使用 zPosition 如下

salonDetailView.layer.zPosition = 1

回答by Peter

Or use this

或者用这个

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

as per here

按照这里