user2636197 user2636197 - 4 years ago 204
iOS Question

Swift ios only hide UIView or also hide elements inside UIView

In one viewcontroller I have a few

UIViews
that constain some text labels and buttons. Depending on what data I receive from my API I either hide some of the
UIViews
or populate them with data.

My question now is, can I just hide the
UIView
or must I also hide all of the elements that are nested inside the
UIView
?

Eg:

myView.hidden = true
myView.userinteractionsEnabled = false


vs

myView.hidden = true
myView.userinteractionsEnabled = false

// And some stuff inside/nested myView
myButton.hidden = true
myButton.userinteractionsEnabled = false
myLabel.hidden = true

Answer Source

It hides the subviews too.

You can test this easily enough in a Playground:

import UIKit

var v = UIView(frame: CGRectMake(0,0, 600, 600))
v.backgroundColor = UIColor.redColor()

var subv = UIView(frame: CGRectMake(100,100, 200, 200))
subv.backgroundColor = UIColor.blueColor()

var subv2 = UIView(frame: CGRectMake(10,10, 50, 50))
subv2.backgroundColor = UIColor.whiteColor()

subv.addSubview(subv2)


v.addSubview(subv)

subv.hidden = true
v

Here is the result:

Subviews in a playground

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download