2016-06-13 83 views
0

我需要根據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]; 

回答

0

問題是logoView是一個局部變量。所以[logoView removeFromSuperview]將無法​​正常工作。

logoView一個物業類變量將修復它。

0

創建全局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)]; 
} 
} 
0

[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 
0

設置標籤去除的UIView。我希望它將對你有用

logoView.tag = 108; //set the tag 
[[self.view viewWithTag:108]removeFromSuperview]; //remove the view the superview 

或變量分配給公衆「logoView」