ios 当你点击它时如何将子视图带到前面?

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

How to bring a subview to the front when you tap it?

ios

提问by MusiGenesis

I have a view with about 30 subviews on it. I want to add code in my subview's viewcontroller so that when I tap the subview, the subview expands to fill the screen.

我有一个视图,上面有大约 30 个子视图。我想在我的子视图的视图控制器中添加代码,以便当我点击子视图时,子视图扩展以填充屏幕。

I'm using [self.view setFrame:rect]to expand the subview, and this works fine. The problem is that some of the other 29 subviews are above the subview that I just tapped, so they're still visible.

我正在使用[self.view setFrame:rect]扩展子视图,这很好用。问题是其他 29 个子视图中的一些位于我刚刚点击的子视图之上,因此它们仍然可见。

I've tried using bringSubviewToFrontfrom the parent viewcontroller, but this appears to have no effect. Is there any code I can put in my subview's viewcontroller class to make this happen?

我试过bringSubviewToFront从父视图控制器使用,但这似乎没有效果。我可以在子视图的 viewcontroller 类中放入任何代码来实现这一点吗?

回答by seanalltogether

bringSubviewToFront should work, just make sure you're using it correctly. The usage is.

BringSubviewToFront 应该可以工作,只需确保您正确使用它即可。用法是。

[parentView bringSubviewToFront:childView];

回答by Vikram Biwal

Use this code, even you don't know about superView:

使用此代码,即使您不了解 superView:

In objective-c:

在目标-c 中:

[subview.superview bringSubviewToFront: subview];

In swift:

迅速:

subview.superview?.bringSubview(toFront: subview)

回答by Martin Metselaar

For the ease of use an extension in Swift 3

为了便于使用Swift 3 中的扩展

extension UIView {

    func bringToFront() {
        self.superview?.bringSubview(toFront: self)
    }

}

回答by Rohan Dave

In Objective-C:

在 Objective-C 中:

[self.superview bringSubviewToFront: subview];

In Swift 2.3:

在 Swift 2.3 中:

self.superview!.bringSubviewToFront(subview)

In Swift 3.1.1 - Swift 4.1:

在 Swift 3.1.1 - Swift 4.1 中:

superview.bringSubview(toFront: subview)