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
How to set iPhone UIView z 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 zPosition
property of the view's layer (it's a CALayer
object) 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
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:
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 subviews
array. 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,
考虑到流布局,黄色是父视图,红色、绿色和蓝色是黄色的兄弟子视图,
the goal is to move a subview (let's say green) to the 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.
在界面生成器中,您需要做的就是将要显示在文档大纲中列表顶部的视图拖到底部。
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,bringSubviewToFront
moves thegreenView
from index0
to2
. This can be observed withprint(yellowView.subviews.indexOf(greenView))
- 此方法是上述 IB 答案的编程等效方法。
- 仅当子视图是彼此的兄弟时才有效。
子视图的数组包含在
yellowView.subviews
. 在这里,bringSubviewToFront
将greenView
from 索引移动0
到2
. 这可以观察到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 thegreenView
looks like it is on top. However, it still remains at index0
of theyellowView.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 toCGFloat.greatestFiniteMagnitude
(CGFloat(FLT_MAX)
in older versions of Swift) to ensure that it is on top.
- 此方法只是将图层的 3D 位置在 z 轴上移得更高(更靠近用户)。由于默认值
0
适用于所有其他视图,因此结果greenView
看起来像是在顶部。但是,它仍然保留在数组的索引0
处yellowView.subviews
。但是,这可能会导致一些意想不到的结果,因为诸如点击事件之类的事情仍将首先进入具有最高索引号的视图。因此,最好采用上面的方法 1。 - 将
zPosition
可以设置为CGFloat.greatestFiniteMagnitude
(CGFloat(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!
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