ios 将 UIButton 带到前层

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

Bring UIButton to front layer

iosobjective-cuibutton

提问by Ryan

I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.

我知道有一种方法可以将子视图带到层次结构的前面。但是,有没有一种方法可以将我放置在故事上的按钮带入并调用一个函数,该函数将使该按钮成为顶层,因此它不会被添加到故事板中的任何其他元素隐藏。

Thanks,

谢谢,

回答by Ryan

I am late for this question, but not too late to answer it by Swift 3

我对这个问题迟到了,但 Swift 3 回答这个问题还为时不晚

Swift 3

斯威夫特 3

 view.bringSubview(toFront: theButton)

回答by Gereon

A UIButton is a UIView, so you can call -bringSubviewToFront:on your button instance.

UIButton 是一个 UIView,因此您可以调用-bringSubviewToFront:您的按钮实例。

回答by dreamweiver

uiview's are displayed in the order they are stacked. One can change this to say were to insert the new uiview, which in your case is a uibutton.

uiview的按堆叠顺序显示。可以将其更改为插入 new uiview,在您的情况下是一个 uibutton 。

One way of doing that is by creating a IBOUtletof the uibuttonand adding that button instance as a subview/newview above an existing uiview.

这样做的一种方式是通过创建IBOUtletuibutton和补充说按钮实例作为一个子视图/ NewView的现有以上uiview

CODE:

代码:

[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];

[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];

Also if you are not able to figure out the order of the uiview, then you can check that in xcode

此外,如果您无法弄清楚 的顺序uiview,那么您可以在xcode

  • When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)
  • 在调试窗口下以调试模式运行项目时,有一个按钮(在下图中突出显示)Debug View Hierarchy,您需要单击它。(在下图中突出显示)

Debug window tool bar

调试窗口工具栏

  • After that you will able to see all the viewsrendering on the screen at current instance, using this feature you will be able to understand were is your new uiviewin the uiview stack order(shown below).
  • 之后,你将能看到所有views在屏幕上呈现在目前情况下,使用此功能,您将能够理解为是新uiviewuiview stack order(如下图所示)。

UI View Hierarchy

UI 视图层次结构

回答by Intentss

Swift

迅速

The following works great if the view you are working with is being hidden views that are in the same view.

如果您正在使用的视图是同一视图中的隐藏视图,则以下内容非常有用。

superview?.bringSubviewToFront(self)

回答by Dave Levy

Swift 5

斯威夫特 5

In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.

就我而言,我不得不在一些按钮后面插入我的视图。通过将其插入零位置,它被放置在按钮后面。

        self.view.insertSubview(self.mapView!, at: 0)  

回答by dimaskpz

i have the same problem with you and a friend of mine helped me

我和你有同样的问题,我的一个朋友帮助了我

i can bring my button to another subview using this function on your view controller where you declare your button.

我可以在您声明按钮的视图控制器上使用此函数将我的按钮带到另一个子视图。

    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.keyWindow?.addSubview(chatButton)
    }