2016-06-13 13 views
0

Ich muss abhängig von der UIInterfaceOrientation unterschiedliche Größe eines UIImage anzeigen. Jetzt ist das Problem, jedes Mal, wenn das Programm seine Ausrichtung ändert, ist das Bild immer noch da und das neue überlappt es. Hier ist, was ich habeUIView wird nicht entfernt

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]; 

Antwort

0

Das Problem ist logoView eine lokale Variable ist. also [logoView removeFromSuperview] wird nicht funktionieren.

Machen logoView ein Property oder eine Klassenvariable werden es beheben.

0

schaffen globale logoView

, wenn es nicht null entfernt sie aus dem Superview ist

if(logoView){ 

    [logoView removeFromSuperview]; 
logoView = nil; 
    } 

und wenn es null ist dann nur intialize es

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]; nicht die alten nicht entfernt logoView.In der Tat, wenn Sie nicht das LogoView als eine Eigenschaft in Ihrem Controller, erhalten Sie nicht das LogoView und entfernen Sie es von self.view.

@interface ViewController() 

@property (nonatomic, strong) UIView *logoView;//You can keep it and get it when you want to remove it. 

@end 

Dann:

[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

den Tag Stellen Sie die UIView zu entfernen. Ich hoffe, dass es nützlich für Sie

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

ODER die Variable public "logoView" zuweisen