Swift:查看隐藏按钮

时间:2018-06-12 20:34:50

标签: ios swift view storyboard subview

所以在故事板中,我已经将视图放到了我的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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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

This post might be helpful

答案 1 :(得分:0)

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

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]
}

我们看到你宣布了烟雾的框架。好吧,似乎你的烟雾变量(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个按钮。