如何设置iPhone UIView z索引?
我想将一个视图移动到另一个视图之上,如何知道视图的 z 索引,以及如何移动到顶部?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想将一个视图移动到另一个视图之上,如何知道视图的 z 索引,以及如何移动到顶部?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
您可以使用视图图层的
zPosition
属性(它是一个CALayer
对象)来更改视图的 z-index。Viktor Nordling 补充道,“大值位于顶部。您可以使用任何您想要的值,包括负值。 ”默认值为0。
需要导入QuartzCore框架才能访问该层。只需将这行代码添加到实现文件的顶部即可。
You can use the
zPosition
property of the view's layer (it's aCALayer
object) to change the z-index of the view.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.
UIView
同级按照添加到其超级视图的顺序堆叠。UIView
层次结构方法和属性用于管理视图顺序。在 UIView.h 中:同级视图在
subviews
数组中从后到前排序。所以最上面的视图将是:底部视图将是:
就像 Kolin Krewinkel 所说,
[parentView BringSubviewToFront:view]
会将视图带到顶部,但只有当视图都是时才会出现这种情况等级制度中的兄弟姐妹。UIView
siblings are stacked in the order in which they are added to their superview. TheUIView
hierarchy methods and properties are there to manage view order. In UIView.h:The sibling views are ordered back to front in the
subviews
array. So the topmost view will be:and bottom view will be:
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.IB 和 Swift
鉴于流动布局,其中黄色是超级视图,红色、绿色和蓝色是黄色的同级子视图,
目标是移动子视图(让我们说绿色)到顶部。
在 Interface Builder 中
在 Interface Builder 中,您所需要做的就是将要在顶部显示的视图拖到文档大纲中列表的底部。
或者,您可以选择视图,然后在菜单中转到“编辑器 安排>发送到前面。
在 Swift 中,
有几种不同的方法可以通过编程来完成此操作。
方法 1
此方法与上面的 IB 答案在编程上等效。
仅当子视图互为同级时才有效。
子视图数组包含在
yellowView.subviews
中。此处,bringSubviewToFront
将greenView
从索引0
移动到2
。这可以通过观察方法 2
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,
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.
In Swift
There are a couple of different ways to do this programmatically.
Method 1
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 withMethod 2
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.zPosition
could be set toCGFloat.greatestFiniteMagnitude
(CGFloat(FLT_MAX)
in older versions of Swift) to ensure that it is on top.如果您想通过 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.
在您想要到达顶部的视图中......
(快速)
Within the view you want to bring to the top...
(in swift)
我们可以在 ios 中使用 zPosition
如果我们有一个名为 salonDetailView 的视图,
例如:
@IBOutlet weak var salalDetailView: UIView!
就我而言,请参阅图像
并拥有 GMSMapView 的 UIView
例如:
@IBOutlet weak var mapViewUI: GMSMapView!
要显示mapViewUI上方的视图salonDetailView
使用zPosition如下
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
如果您使用的是 cocos2d,您可能会看到 [parentView BringSubviewToFront:view] 的问题,至少它对我不起作用。我没有将我想要的视图放在前面,而是将其他视图发回,这就达到了目的。
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.
使用它
或者按照此处
Or use this
as per here