Swift: 查看隐藏按钮

Swift: view hides button

所以在情节提要中,我对我的 viewcontroller 做了一个视图。

这个视图还有一个自定义颜色,在我的代码中定义:

 func setBlueGradientBackground(){
    let topColor = UIColor(red: 95.0/255.0, green: 165.0/255.0, blue: 1.0, alpha: 1.0).cgColor
    let bottomColor = UIColor(red: 72.0/255.0, green: 114.0/255.0, blue: 184.0/255.0, alpha: 1.0).cgColor
    smoke.frame = view.bounds
    smoke.colors = [topColor, bottomColor]

我的 viewdidload 看起来像这样:

override func viewDidLoad() {
    super.viewDidLoad()


    setBlueGradientBackground()
    lava.layer.addSublayer(smoke)
}

现在我想在我的视图上添加一个按钮。

视图以某种方式隐藏了按钮。

如果我删除

setBlueGradientBackground() 

函数,有效..

编辑:按钮在颜色正在改变的视图中。

https://imgur.com/a/9okrll6

我该如何解决这个问题?

也许按钮在您的子视图后面?您是否尝试过更改视图对象的堆叠顺序?

This post might be helpful

所以检查一下。在你的方法中

func setBlueGradientBackground(){
    let topColor = UIColor(red: 95.0/255.0, green: 165.0/255.0, blue: 1.0, alpha: 1.0).cgColor
    let bottomColor = UIColor(red: 72.0/255.0, green: 114.0/255.0, blue: 184.0/255.0, alpha: 1.0).cgColor
    smoke.frame = view.bounds //HERE SEEMS TO BE CULPRIT
    smoke.colors = [topColor, bottomColor]
}

我们看到您声明了 smoke 的框架。好吧,您的烟雾变量(UIView 的某些血统)似乎已添加到按钮后某处的屏幕上。所以也许这样的事情发生了

var smoke:UILabel = UILabel()

func viewDidLoad() {
    var button = UIButton(frame: CGRect(x: self.view.frame.width*0.3, y: self.view.frame.height*0.3, width: self.view.frame.width*0.4, height: self.view.frame.height*0.4))
    self.view.addSubview(button) //ADDED BEFORE
    self.view.addSubview(smoke) //ADDED AFTER
}

现在 setBlueGradientBackground 会解决这个问题的原因是你在里面设置了 smoke's frame。所以,如果我们删除 setBlueGradientBackground,我们也不接受烟雾的框架。烟雾现在不在屏幕上,即使它被添加为视图;它没有界限。因此,尽管按钮是在您的按钮之后添加的,但它不能阻止任何内容,因为它没有边界。

这是一个快速查看图层位置的小工具。在 XCode 中,当你 运行 程序时,在调试器工具上你有这些按钮 -> Breakpoints、Pause、Skip、Inside、Something Else,然后你有一行看起来像这样的 |。然后你有另一个按钮,看起来像 2 个矩形,1 个宽度和 1 个高度组合,单击它,它实际上会在给定状态下暂停你的程序并显示层的位置。这很漂亮。就是下图第6个按钮