kahlo
kahlo

Reputation: 2374

Change a subview at run time in iOS

In the initialization of my view controller I declare a view and I set it as a subview of the main view:

self.customView = [[UIView alloc] initWithFrame:self.view.frame];
self.customView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.customView];

Later on, in a method I need to substitute self.customView for another view. (Note: changing the background color is just a simplified example. The views are more complex than that).

UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];
anotherView.backgroundColor = [UIColor blackColor];
self.customView = anotherView;

But this has no effect. However if I instead make something like:

[self.view addSubview:anotherView];

It works fine. I want however to get rid of the previous view without localizing and removing the view explicitly. Isn't it possible to substitute a subview at runtime or I am missing something?

I work with ARC. Thanks!

Upvotes: 2

Views: 7156

Answers (4)

Flop
Flop

Reputation: 153

I suppose the best solution for you it is to write a custom setter to the @property customView:

in header:

@property (nonatomic, strong) UIView* customView;

in impelementation:

@synthesize customView = _customView;
...
-(void) setCustomView:(UIView *)customView {
    NSUInteger z = NSNotFound;
    if (_customView) {
        z = [self.view.subviews indexOfObject:_customView];
    }
    if (z == NSNotFound) {
        // old view was not in hierarchy
        // you can insert subview at any index and any view you want by default
        [self.view addSubview:customView];
    } else {
        // you can save superview
        UIVIew *superview = _customView.superview;
        [_customView removeFromSuperview];
        //also you can copy some attributes of old view:
        //customView.center = _customView.center
        [superview insertSubview:customView atIndex:z];
    }
    // and save ivar
    _customView = customView;
}

So you will not need to add your customView as subview. Also you will have the ability to replace your customView at any moment

Upvotes: 4

Prashanth Rajagopalan
Prashanth Rajagopalan

Reputation: 726

The example code you have shared I feel it is not the right way to code. Follow the sample code.

initialize both the view and set the hidden property

self.customView = [[UIView alloc] initWithFrame:self.view.frame];

self.customView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.customView];

self.customView.hidden = YES;

UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];

anotherView.backgroundColor = [UIColor blackColor];

[self.view addSubview:anotherView];

anotherView.hidden = YES;

During runtime choose your required view by

self.customView.hidden = NO;

anotherView.hidden = YES;

if you need self.customView to show

AND

anotherView.hidden = NO;

self.customView.hidden = YES;

if you need anotherView to show

Upvotes: 0

Manish Agrawal
Manish Agrawal

Reputation: 11026

instead of removing the old one and adding new one you can change the color of the old one, this will be more optimized rather that creating/removing view.

You can change the color of the view that is already added by following code

self.customView.backgroundColor = [UIColor blackColor];

Upvotes: 0

rmaddy
rmaddy

Reputation: 318774

You need to remove the old view and add the new view. Something like this:

// Remove old customView
[self.customView removeFromSuperview];

// Add new customView
UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];
anotherView.backgroundColor = [UIColor blackColor];
self.customView = anotherView;
[self.view addSubview:self.customView];

Edit:

I didn't even notice that all you are doing is changing the background color. In this case there is no need to replace the view at all. Just update the background color:

self.customView.backgroundColor = [UIColor blackColor];

Upvotes: 2

Related Questions