ios - 如何在 Swift 中向后发送 UIView 元素

标签 ios iphone swift uiview

所以我在 main.Storyboard 中添加了一个按钮,以及 View 的主背景,然后在 viewController.swift 文件中,我创建了一个 UIView 矩形形状,我想将其放在该按钮后面(作为它的背景)。

问题是,当我添加矩形 UIView 时,它总是将它添加到按钮前面。所以我在网上研究并找到了 sendSubviewToBack 代码。我已经添加了它,但它一直将它发送到 View 的主要 UIImage 背景后面。

有没有一种方法可以将这个 UIView 发送到按钮后面但在 UIImage 背景前面?

func nextBarDisplayed() {

    let theHeight = self.view.frame.height
    nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
    nextBar.frame = CGRect(x: 0, y: theHeight - 50, width: self.view.frame.width, height: 50)
    self.view.insertSubview(nextBar, aboveSubview: myBackgroundView)

}

最佳答案

来自 apple documentation :

bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)

您可以使用以下方法将栏置于最前面:

view.bringSubviewToFront(nextBar) 

您使用以下方式将您的 View 发送到栏的后面:

nextBar.view.sendSubviewToBack(myView)

关于ios - 如何在 Swift 中向后发送 UIView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769052/

相关文章:

ios - 如何完全覆盖导航栏的后退按钮操作?

ios - 仅 cellForRowAt indexPath 未调用所有其他数据源和委托(delegate)

ios - 为 UITextView 中的文本获取 CGRect 以使用 CALayer 突出显示

iOS:如何关闭显示的 Leadbolt 广告的添加事件?

iphone - UITapGestureRecognizer 操作未触发; userInteractionEnabled 已设置为 YES

ios - 操作系统状态错误 1685348671 AVAudioPlayer

ios - 用覆盖层覆盖 UIAppDelegate 的窗口使用户在显示时无法触摸 UIActivityViewController 的扩展

objective-c - objective-c :按下按钮后切换属性的BOOLEAN值的问题

android - 将一个移动应用程序与其他应用程序(Android 和 IOS)集成

ios - Swift:我想从 csv 文件中读取具有许多属性的国家/地区列表,并将其用于我的应用程序。我怎样才能实现这个?