Question

I have an universal application and the frame of the subviews in the code set for iPhone, and I saw that putting the iPad simulator the subviews are the same size. Therefore the iPad I have to rewrite all the frame or are there shortcuts? This is the code.

 -(void) layoutPortrait {

self.imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_V.png"]];
self.imageView.frame= CGRectMake(0, 0, 320, 80);
self.labelTitle.frame =CGRectMake(14, 103, 91, 54);
self.buttonFavorites.frame= CGRectMake(109, 485, 33, 33);
self.buttonHome.frame =CGRectMake(14, 485, 33, 33);
self.buttonMap.frame=CGRectMake(61, 485, 33, 33);


 }

 -(void) layoutLandscape {

self. imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_O.png"]];
self.imageView.frame= CGRectMake(0,0,568,65 );
self.labelTitle.frame =CGRectMake(20,92,165,48);
self.buttonFavorites.frame= CGRectMake(107,237,33,33);
self.buttonHome.frame =CGRectMake(11,237,33,33);
self.buttonMap.frame=CGRectMake(59,237,33,33);



 }


 - (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden=YES;
// Do any additional setup after loading the view from its nib.

//orientazioni
UIInterfaceOrientation deviceOrientation= self.interfaceOrientation;


if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
    [self layoutPortrait];

}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
    [self layoutLandscape];

}
else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){

    [self layoutLandscape];



}


[self.view addSubview:self.labelTitle];
[self.view addSubview:self.imageView];
[self.view addSubview:self.buttonFavorites];
[self.view addSubview:self.buttonHome];
[self.view addSubview:self.buttonMap];
}
Was it helpful?

Solution

There are two ways to go about this.

Firstly, you could create frames specific for the iPad, and the iPhone 5.

Secondly, you make views automatically resize according to the parent view, or to other subviews. This is done with autoresizing. A nice link which describes this: iOS 5 iPhone Rotation, View Resizing and Layout Handling

I noticed that your code uses the old way of laying out views, i.e. detect the interface orientation and then laying out the frames according to that. The better way, which is heavily emphasized since iOS 6, is to use layoutSubviews() and companions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top