Matei Beloiu
Matei Beloiu

Reputation: 81

Adding a UIView onto another

I am starting out with iOS programming and I am wondering how one can add a UIView on top of another and programmatically hide it or show it.

Upvotes: 1

Views: 290

Answers (1)

Abizern
Abizern

Reputation: 150615

Create a new frame for the view that you want to add, based on the coordinate system of the view that you are adding it to - this is how you position your new view. Then you just add the view as a subview

[mainView addSubview:newView];

To get rid of it:

[newView removeFromSuperview];

This will remove the subview and release it, so if you only want to hide or show it, make sure you retain the subview (or if you are using ARC, maintain a strong reference to it) that way you don't have to create / destroy the subview just to show or hide it.

Upvotes: 2

Related Questions