【问题标题】:SubView added to View but not showing子视图添加到视图但不显示
【发布时间】:2020-01-05 12:57:51
【问题描述】:

我正在尝试显示已添加到视图中的子视图,但按下按钮时它不显示。

我尝试将 isOpaque 设置为 1,将 alpha 设置为 1,将 isHidden 设置为 false(无需按下按钮)并检查我是否运行了 view.addSubview()。我还发现子视图根本没有隐藏,但背景是白色的(应该是蓝色或红色)。

添加子视图的代码

//setup
viewBGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewBGK = UIView(frame: viewBGKRect)
viewBGK.backgroundColor = UIColor(red: 139.0, green: 206.0, blue: 231.0, alpha: 1.0)
viewBGK.alpha = 1
viewBGK.isOpaque = true

viewRGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewRGK = UIView(frame: viewRGKRect)
viewRGK.backgroundColor = UIColor(red: 240.0, green: 177.0, blue: 187.0, alpha: 1.0)
viewRGK.alpha = 1
viewRGK.isOpaque = true

//isHidden is set to false when the buttons are pressed
viewBGK.isHidden = true
viewRGK.isHidden = true

view.addSubview(viewBGK)
view.addSubview(viewRGK)

显示子视图的代码

@IBAction func goalkeeper(_ sender: UIButton) {
        switch sender.tag {
        case 0:
            // blue
            viewBGK.isHidden = false
            viewRGK.isHidden = true
            return
        default:
            viewBGK.isHidden = true
            viewRGK.isHidden = false
            return
        }
    }

我希望屏幕顶部会出现一个蓝色/红色矩形,但它没有显示。

【问题讨论】:

  • 你把“添加子视图的代码”放在哪里了? viewDidLoad?另外,什么视图没有显示?蓝色或红色或两者兼而有之?

标签: swift addsubview


【解决方案1】:

没关系,我找到了答案: UIColor RGB 是从 0-1 而不是 0-255 颜色应该是

(蓝色)

UIColor(red:0.55, green:0.81, blue:0.91, alpha:1.0)

(红色)

UIColor(red:0.94, green:0.69, blue:0.73, alpha:1.0)

不是

(蓝色)

UIColor(red: 139.0, green: 206.0, blue: 231.0, alpha: 1.0)

(红色)

UIColor(red: 240.0, green: 177.0, blue: 187.0, alpha: 1.0)

我现在真的很笨。

【讨论】:

  • 您自己找到了答案,做得很棒。这是另一种获得颜色的方法,无需进行所有数学运算即可获得小数:youtube.com/watch?v=kTiL4Xtwg84
  • 干得好,找到问题的解决方案。如果它有效,您应该将其标记为答案。
【解决方案2】:

如果您要使用自定义颜色,则在视图控制器以外的地方声明它们可能更容易。一种方法是在扩展中声明它们。为此,您需要执行以下操作:

  1. 创建一个新的 Swift 文件并将其命名为 UIColor+Extension.swift

  2. 在新文件中,添加以下代码:

    extension UIColor {
        static var customBlue: UIColor {
            return #colorLiteral(red: 0.5450980392, green: 0.8078431373, blue: 0.9058823529, alpha: 1)
        }
    
        static var customRed: UIColor {
            return #colorLiteral(red: 0.9411764706, green: 0.6941176471, blue: 0.7333333333, alpha: 1)
        }
    }
    

我没有把这些颜色打出来。我只是输入了return Color Literal,它显示了一个白色的圆角矩形。当我点击矩形时,我看到了这个:

然后,我单击“其他”按钮并输入 RGB 值:

最后,您要避免编写重复的代码(DRY = 不要重复自己)。这是更新的代码:

//setup
viewBGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewBGK = UIView(frame: viewBGKRect)
viewBGK.backgroundColor = .customBlue

viewRGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewRGK = UIView(frame: viewRGKRect)
viewRGK.backgroundColor = .customRed

[viewBGK, viewRGK].forEach { view in
    view.alpha = 1
    view.isOpaque = true
    //isHidden is set to false when the buttons are pressed
    view.isHidden = true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多