如何更改 UIView 的 z 索引或堆栈顺序? [英] How do I change the z index or stack order of UIView?

查看:29
本文介绍了如何更改 UIView 的 z 索引或堆栈顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了创建渐变层,我在现有控制器的顶部堆叠了一个 UIView.但是,当我加载视图时,新的 UIView(自然)位于一切之上.

相关代码:

class ViewController: UIViewController {@IBOutlet 弱 var myGradientView:GradientView!覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func viewDidLayoutSubviews() {self.myGradientView.gradientWithColors(UIColor.whiteColor(), UIColor.blueColor())}}

我做了一些搜索,我在目标中找到了一个答案-C 说明使用:

<块引用>

[parentView 带来SubviewToFront:view];

我不知道objective-c并且对Swift相当陌生,如果有人能对此有所启发,我将不胜感激.

我的问题:

如何在 Swift 中更改 UIView 的 z 索引或堆栈顺序?

解决方案

我建议查看 UIView 文档,其中列出了几种操作子视图顺序的方法:

<块引用>

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

在您的情况下,您可以尝试:

self.view.sendSubviewToBack(myGradientView)//在这种情况下,self 是你的视图控制器.

或者,因为您在 IB 中创建了 myGradientView,您可以更改视图层次结构以更改显示在顶部的视图.这是一张图片来说明:

希望有所帮助.

I've stacked a UIView on top of an existing controller in an effort to create a gradient layer. However, when I load the view the new UIView (naturally) is on top of everything.

Relevant Code:

class ViewController: UIViewController {

    @IBOutlet weak var myGradientView: GradientView!

    override func viewDidLoad() {
        super.viewDidLoad()            
    }

    override func viewDidLayoutSubviews() {
        self.myGradientView.gradientWithColors(UIColor.whiteColor(), UIColor.blueColor())
    }
}

I've done some searching and I found an answer in Objective-C that explains to use:

[parentView bringSubviewToFront:view];

I don't know objective-c and am fairly new to Swift, if someone could shine some light on this I would appreciate it.

My Question:

How do I change the z index or stack order of a UIView in Swift?

解决方案

I would recommend looking in the UIView documentation, where are several methods listed for the manipulation of the order of subviews:

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

In your situation, you could try:

self.view.sendSubviewToBack(myGradientView)
// self is your view controller in this case.

Alternatively, because you created the myGradientView in IB, you could change the view hierarchy to change which views appeared on top. Here's a picture to illustrate:

Hope that helps.

这篇关于如何更改 UIView 的 z 索引或堆栈顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆