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

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

问题描述

我在现有控制器的顶部堆叠了一个UIView,以创建一个渐变层。但是,当我加载视图时,新的UIView(自然地)就在所有内容之上。

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.

相关代码:

class ViewController: UIViewController {

    @IBOutlet weak var myGradientView: GradientView!

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

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

我已经做了一些搜索,我找到了目标中的答案-C 解释使用:

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


[parentView bringSubviewToFront:view];

[parentView bringSubviewToFront:view];

我不知道Objective-c并且对Swift来说还是新手,如果有人可以对此有所启发,我会很感激。

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

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

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

推荐答案

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

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.

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

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:

希望有所帮助。

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

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