xcode 如何以编程方式将视图设置为前/后?

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

How to programmatically set view to the front/back?

iosswiftxcodeuikituiview-hierarchy

提问by Ross Sullivan

I'm working on an application that uses UIViews as backgrounds to text. I've put the views behind the text in the storyboardbut for some reason some still seem to be in front of the text. Is there a way to send these view to the back programmatically? Here is a screenshot of what im talking aboutPicture

我正在开发一个使用UIViews 作为文本背景的应用程序。我已经把这些观点放在了文本后面,storyboard但出于某种原因,有些观点似乎仍然在文本前面。有没有办法以编程方式将这些视图发送到后面?这是我在说什么的截图图片

回答by Harsh

You can use on of these two:

您可以使用以下两个中的一个:

- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;

Objective-C Usage

Objective-C 的用法

view.sendSubviewToBack(subview)and view.bringSubviewToFront(subview)

view.sendSubviewToBack(subview)view.bringSubviewToFront(subview)

SWIFT 4.2 Usage

SWIFT 4.2 用法

view.sendSubview(toBack: subview)
view.bringSubview(toFront: subview)
  • bringSubviewToFront: Moves the specified subview so that it appears on top of its siblings.
  • sendSubviewToBack: Moves the specified subview so that it appears behind its siblings.
  • BringSubviewToFront:移动指定的子视图,使其出现在其兄弟视图的顶部。
  • sendSubviewToBack:移动指定的子视图,使其出现在其兄弟视图的后面。

https://developer.apple.com/reference/uikit/uiview?language=objc

https://developer.apple.com/reference/uikit/uiview?language=objc

回答by Evgeny Karkan

sendSubviewToBackAPI is what you want for this task.

sendSubviewToBackAPI 是您在此任务中想要的。

Moves the specified subview so that it appears behind its siblings.

This method moves the specified view to the beginning of the array of views in the subviews property.

移动指定的子视图,使其出现在其兄弟视图的后面。

此方法将指定的视图移动到 subviews 属性中的视图数组的开头。

UIViewclass reference

UIView类参考

Below is a code snippet in Swiftlang of how to call this API:

以下是Swift如何调用此 API 的 lang代码片段:

Swift

迅速

yourSuperview.sendSubviewToBack(yourSubview)