drbj drbj - 1 year ago 90
iOS Question

UIView is not being removed

I need to display different size of a UIImage depending on the UIInterfaceOrientation. Now the problem is, everytime the program changes its orientation the image is still there and the new one is overlapping it. Here's what I have

UIView *logoView;

UIImage *image = [UIImage imageNamed:@"pic.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[logoView removeFromSuperview];
CGRect rect;
UIInterfaceOrientation newOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight)
rect = CGRectMake(0, 0, self.view.bounds.size.width/2, self.view.bounds.size.height/4);
logoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width/2, self.view.bounds.size.height/4)];

else if (newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/4);
logoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height/4)];
imageView.frame = logoView.bounds;
[logoView addSubview:imageView];

[self.view addSubview:logoView];

Answer Source

The problem is logoView is a local variable. so [logoView removeFromSuperview] will not working.

Make logoView a Property or a class variable will fix it.

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