ios 如何在 Swift 中将视图置于另一个视图之前?

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

How can I bring a view in front of another view, in Swift?

iosswiftinterface-builder

提问by vk2015

Below are how my views are organized in IB, top->bottom when the app is started.

下面是我的视图在 IB 中的组织方式,当应用程序启动时,顶部->底部。

The user can do something to make "Category Table View Header" temporarily expand over "Name View" - however once doing so, the .TouchDown action assigned to "Category Table View Header" no longer works wherever it overlaps with "Name View" (i.e., the user can tap the header anywhere it doesn't overlap with name view and it still works).

用户可以做一些事情使“类别表视图标题”暂时扩展到“名称视图” - 但是一旦这样做,分配给“类别表视图标题”的 .TouchDown 操作在与“名称视图”重叠的任何地方都不再起作用(即,用户可以点击标题与名称视图不重叠的任何地方,它仍然有效)。

enter image description here

在此处输入图片说明

I know that may be confusing, so I drew out some boxes. On the left is the original, right is after user action - problem is on the right the action on the red box only works if the user taps the bottom half, not the top half.

我知道这可能会令人困惑,所以我画了一些盒子。左边是原始的,右边是用户操作之后——问题出在右边,红框上的操作只有在用户点击下半部分时才有效,而不是上半部分。

enter image description here

在此处输入图片说明

My guess is its because the header is lower in the view hierarchy than the name view, but it would be hard for me to change that without messing around with a bunch of constraints.

我的猜测是因为标题在视图层次结构中比名称视图低,但是我很难改变它而不会弄乱一堆约束。

I also tried setting nameView.hidden = true, but that doesn't work.

我也尝试设置 nameView.hidden = true,但这不起作用。

回答by Chathuranga Silva

If you want to bring a subview to the front, you can use:

如果你想把一个子视图放在前面,你可以使用:

SWIFT 4 UPDATE

SWIFT 4 更新

self.view.bringSubviewToFront(yourView)

SWIFT 3 UPDATE

SWIFT 3 更新

self.view.bringSubview(toFront: yourView)

Send view to back:-

将视图发送回:-

SWIFT 4 UPDATE

SWIFT 4 更新

self.view.sendSubviewToBack(yourView)

SWIFT 3 UPDATE

SWIFT 3 更新

self.view.sendSubview(toBack: yourView)

回答by Vinoth Vino

In Swift 5

斯威夫特 5

To bring a subview to front

将子视图置于前面

self.view.bringSubviewToFront(yourView)

To send a subview to back

返回子视图

self.view.sendSubviewToBack(yourView)

回答by Yodagama

Swift 5.1

斯威夫特 5.1

UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront(). Here's an example:

UIKit 将视图从后向前绘制,这意味着堆栈较高的视图绘制在较低的视图之上。如果你想把一个子视图放在前面,有一个方法适合你:bringSubviewToFront()。下面是一个例子:

parentView.bringSubviewToFront(childView)

This method can also be used to bring any subview to the front, even if you're not sure where it is:

这个方法也可以用来把任何子视图放在前面,即使你不确定它在哪里:

childView.superview?.bringSubviewToFront(childView)

回答by tosha

You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.

您可以使用方法控制子视图的顺序:从超级视图中引入子视图和发送子视图。

You can access all the subviews contained by superview using self.view.subview array.

您可以使用 self.view.subview 数组访问 superview 包含的所有子视图。

回答by K-A

The method has been updated since swift 3

该方法自 swift 3 以来已更新

What has worked for me and hopefully for you is using :

对我有用并希望对你有用的是:

YourView.bringSubview(toFront: yourelementA)
YourView.bringSubview(toFront: yourelementB)

Alternatively you could use the following to send the object that is in the front:

或者,您可以使用以下内容发送位于前面的对象:

YourView.sendSubview(toBack: yourelementC)

I hope this helps

我希望这有帮助