I have a view and I am creating a UILabel programatically and calling it my parameter passing as I need many UILabel text with Different Position frame, so I am calling it by parameter passing.
Using the code below, when I rotate to landscape mode the portrait view Label still appears. How do I overcome this? I need to be done in the below code. I want the position of the Label frame to be changed from portrait view to landscape view.
NOTE:I need to Create many Label,s around 15 to 20, so I am using parameter passing, so that I don’t need to write as many methods for creating each label.
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:@"Arial" size:13];
myLabel.text = labelTitle;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view addSubview:[self createLabel:CGRectMake(450,140,60,20):@"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(450,170,60,20):@"Reviews:"]];
}
else
{
[self.view addSubview:[self createLabel:CGRectMake(650,140,60,20):@"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(650,170,60,20):@"Reviews:"]];
}
return YES;
}
Don’t create them in
shouldAutorotateToInterfaceOrientation, because it will get called everytime the orientation changes, thus creating more labels.Instead create them once in
- (void) loadView, and give them the correct autoResizingMask properties, e.g:This will cause the desired behaviour when the orientation changes: the labels will automatically resize and the whole turning animation will be smooth.
PS: Mind that the way you were creating Labels is causing a memory leak, use the
releasemethod to prevent this.