UIView没有被删除
问题描述:
我需要根据UIInterfaceOrientation显示不同大小的UIImage。现在的问题是,每次程序改变方向时,图像仍然存在,新图像重叠。这里是我的东西UIView没有被删除
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];
答
问题是logoView
是一个局部变量。所以[logoView removeFromSuperview]
将无法正常工作。
让logoView
一个物业或类变量将修复它。
答
创建全局logoView
如果它不是来自上海华
if(logoView){
[logoView removeFromSuperview];
logoView = nil;
}
空删除它,如果它为null,则只有intialize它
if(newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight)
{
rect = CGRectMake(0, 0, self.view.bounds.size.width/2, self.view.bounds.size.height/4);
if(!logoView){
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);
if(!logoView){
logoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height/4)];
}
}
答
[logoView removeFromSuperview];
不会删除旧的logoView。事实上,如果您不将logoView作为控制器中的一个属性,您将无法获得logoView并将其从self.view
中删除。
@interface ViewController()
@property (nonatomic, strong) UIView *logoView;//You can keep it and get it when you want to remove it.
@end
然后:
[self.logoView removeFromSuperview];//get the logoView and remove it.
UIView *logoView;
UIImage *image = [UIImage imageNamed:@"pic.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
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;
self.logoView = logoView;//keep logoView
[logoView addSubview:imageView];
[self.view addSubview:logoView];//add logoView to self.view
答
设置标签去除的UIView。我希望它将对你有用
logoView.tag = 108; //set the tag
[[self.view viewWithTag:108]removeFromSuperview]; //remove the view the superview
或变量分配给公众“logoView”