0

So in storyboard i've put a view on to my viewcontroller.

this view also has a custom color, defined in my code:

 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]

and my viewdidload looks like this:

override func viewDidLoad() {
    super.viewDidLoad()


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

Now i wanted to add a button onto my view.

Somehow the view is hiding the button.

If i remove the

setBlueGradientBackground() 

function, it works..

Edit: the button is in the view who's color is changing.

https://i.stack.imgur.com/upu3r.jpg

How do I fix this?

iOS Koray
  • 41
  • 1
  • 9
  • You haven't provided enough information. What does your view hierarchy look like? Is the button button inside the view who's color you're changing? My guess is that it's UNDER the view, in which case it's covering it. Try using the Xcode debug>debug view hierarchy command to see a 3D presentation of your view hierarchy so you can tell what's on top of what. – Duncan C Jun 12 '18 at 20:40
  • the button is inside the view, who's color is changing. https://imgur.com/a/9okrll6 – iOS Koray Jun 12 '18 at 20:46
  • Probably same issue as https://stackoverflow.com/questions/28765608/why-adding-sublayer-overlaps-subviews – dan Jun 12 '18 at 20:52

2 Answers2

0

Perhaps the button is behind your subview? Have you tried changing the stacking order of your view objects?

This post might be helpful

cgontijo
  • 286
  • 2
  • 7
  • I tried but the "Arrange" button in Editor->Arrange is gresy and I cant click on it – iOS Koray Jun 12 '18 at 20:59
  • Instead of selecting the object from the Document Outline (the pane on your left hand side), try selecting it from the build editor. It should work. – cgontijo Jun 12 '18 at 21:13
0

So check this out. Inside your method

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

we see that you declare smoke's frame. Well, it seems that your smoke variable (of some descent of UIView) was added to the screen somewhere AFTER your button. So maybe something like this happened

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
}

Now the reason that setBlueGradientBackground would FIX this is becuse you set the smoke's frame inside there. So, if we remove setBlueGradientBackground, we also don't accept smoke's frame. Smoke is now NOT ON THE SCREEN even though it is added as a view; it has no bounds. Therefore, although the button is added AFTER your button, it can't block anything because it has no bounds.

A quick little tool to see where the layers are is this. Inside XCode, when you run the program, on the debugger tool you have these buttons -> Breakpoints, Pause, Skip, Inside, Something Else, and then you have a line that looks like this |. Then you have another button that looks like 2 rectangles, 1 width and 1 height combined, click on that and it will actually pause your program in the given state and show you where the layers are. It's pretty nifty. It is the 6th button below in the picture.

impression7vx
  • 1,728
  • 1
  • 20
  • 50