我需要根据UIInterfaceOrientation显示不同大小的UIImage。现在的问题是,每当程序改变其方向时,图像仍然存在,而新图像与其重叠。这就是我所拥有的
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];
答案 0 :(得分:0)
问题是logoView
是一个局部变量。所以[logoView removeFromSuperview]
将无效。
让logoView
属性或类变量会修复它。
答案 1 :(得分:0)
创建全局logoView
如果不为null则从superview
中删除它 if(logoView){
[logoView removeFromSuperview];
logoView = nil;
}
如果它为null则只进行初始化
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)];
}
}
答案 2 :(得分:0)
[logoView removeFromSuperview];
不会删除旧的logoView。事实上,如果您没有将logoView作为控件中的属性,则不会获取logoView并将其从self.view
中删除。< / p>
@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
答案 3 :(得分:0)
设置标记以删除UIView。我希望它对你有用
logoView.tag = 108; //set the tag
[[self.view viewWithTag:108]removeFromSuperview]; //remove the view the superview
或者将变量分配给public&#34; logoView&#34;