Shou Haruka
Shou Haruka

Reputation: 29

SubView added to View but not showing

I'm trying to show a subview that has been added to a view, but it does not show up when the button is pressed.

I have tried setting isOpaque to 1, alpha to 1, isHidden to false (without needing to press the button) and have checked that I have run view.addSubview(). I have also found out that the subview is not hidden at all but the background is white (it is supposed to be blue or red).

code to add subviews

//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)

code to show subviews

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

I expect a blue/red rectangle to appear at the top of the screen but it does not show.

Upvotes: 0

Views: 1741

Answers (2)

Adrian
Adrian

Reputation: 16725

If you're going to utilize custom colors, it might be easier to declare them somewhere other than in a view controller. One approach would be to declare them in an extension. To do this, you would do the following:

  1. Create a new Swift file and name it UIColor+Extension.swift

  2. Inside the new file, add the following code:

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

I didn't type those colors out. I simply typed return Color Literal and it showed a white rounded rectangle. When I clicked on the rectangle, I saw this:

enter image description here

Then, I clicked the "Other" button and I typed in the RGB values:

enter image description here

Lastly, you want to avoid writing repetitive code (DRY = don't repeat yourself). Here's the updated code:

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

Upvotes: 2

Shou Haruka
Shou Haruka

Reputation: 29

Nevermind I found the answer: UIColor RGB is from 0-1 not 0-255 the colors should be

(blue)

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

and

(red)

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

not

(blue)

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

and

(red)

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

i feel really dumb now.

Upvotes: 2

Related Questions