Edward Hasted
Edward Hasted

Reputation: 3433

iOS UIView adding and removing subviews throughout the code

I have a blue line in my app that displays an artificial horizon. I want to be able to display and remove it at different parts of the code (.m). Can I declare it (lineViewHorizon) universally.

At present I can make it appear/disappear only within the same section/method of code.

I presume this can be done?

UIView *lineViewHorizon = [[UIView alloc] initWithFrame:CGRectMake(0, pageTopMargin+inthorizon, self.view.bounds.size.width, 2)];
lineViewHorizon.backgroundColor = [UIColor blueColor];
[self.view addSubview:lineViewHorizon];
[lineViewHorizon removeFromSuperview];

Upvotes: 0

Views: 629

Answers (1)

Caleb
Caleb

Reputation: 124997

I want to be able to display and remove it at different parts of the code

In order to remove a view from another view, you'll need a pointer to the view that you want to remove. You can get that either by finding the view in the view hierarchy, perhaps using -viewWithTag:, or you can keep a pointer to the view in an instance variable (or property). Either way, the key thing is that you need a pointer to the view so that you can send it a -removeFromSuperview message.

Upvotes: 1

Related Questions