xcode 关于带来SubviewToFront

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5133532/
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-09-14 20:20:08  来源:igfitidea点击:

About bringSubviewToFront

xcodeios4iphone

提问by SajjadZare

I bring a control to front with the below code:

我使用以下代码将控件置于前面:

[self.view bringSubviewToFront:control];

How can I recognize between two controls; which one is in front and which one is in back

我如何在两个控件之间进行识别;哪个在前面,哪个在后面

采纳答案by Jonathan.

You would check the order of the view in the superview's (of the controls, in your case self.view) subviewsproperty.

您将在超级视图的(控件的,在您的情况下为 self.view)subviews属性中检查视图的顺序。

The view at the index 0 in the superview's subview is the view at the very back, and then the view at index 1 will be on top of it, and the view at index 2 will be on top of the view at index, etc
(basically a view is on top of the views with a lesser index compared to it's own index)

父视图子视图中索引 0 处的视图是最后面的视图,然后索引 1 处的视图将位于其顶部,索引 2 处的视图将位于索引处的视图之上,依此类推
(与它自己的索引相比,基本上一个视图位于索引较小的视图之上)

NSInteger indexOfControl1 = [[self.view subviews] indexOfObject:control1];
NSInteger indexOfControl2 = [[self.view subviews] indexOfObject:control2];
if (indexOfControl1 > indexOfControl2) {
    //control1 is on top of control2
}

回答by Vladimir

If those controls have the same parent view their z-order is defined by their index in subviewsarray:

如果这些控件具有相同的父视图,则它们的 z 顺序由它们在subviews数组中的索引定义:

subviews: The order of the subviews in the array reflects their visible order on the screen, with the view at index 0 being the back-most view.

subviews:数组中子视图的顺序反映了它们在屏幕上的可见顺序,索引 0 处的视图是最后面的视图。

So your steps will be:
1. Get controls
2. Get their indexes in parent's subviews array (using indexOfObject:method)
3. Control with bigger index is in front

所以你的步骤将是:
1. 获取控件
2. 在父视图的子视图数组中获取它们的索引(使用indexOfObject:方法)
3. 具有更大索引的控件在前面