2016-06-13 7 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

それはスーパー

if(logoView){ 

    [logoView removeFromSuperview]; 
logoView = nil; 
    } 

からヌル削除されていない場合は、グローバルlogoView

を作成し、それが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)]; 
} 
} 
0

[logoView removeFromSuperview];は、古いものを削除しませんintialize logoView.LogViewをコントローラ内のプロパティとして保持しないと、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」に変数を割り当てるために有用願っています

関連する問題