user2636197
user2636197

Reputation: 4122

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

Upvotes: 1

Views: 7271

Answers (3)

Jacksmackod
Jacksmackod

Reputation: 87

Hide the view that the other views are inside of. Hiding a view will also hide its subviews.

Upvotes: 1

David S.
David S.

Reputation: 6705

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

Upvotes: 2

stefos
stefos

Reputation: 1235

You just have to hide only the parent view.

Upvotes: 0

Related Questions