Reputation: 359
I am developing an app. In the app I wrote the code for a 3.5-inches screen and 4-inches screen for ios7&ios6. I took one view controller; in this view controller I want to display two images, one for the top and another for the bottom. On the ios7 3.5-inch screen and 4 inches screen there is no problem with the display. But on the ios6 screen for 4-inches screen and 3.5 inches screen, there is a display problem. The two images are not displayed properly. I don't know how to write the same code for both the ios6 and ios7 for 4-inches& 3.5 inches screen. Please give me ideas, anyone. I am new to programming. Thanks in advance.
The below is my code.
Viewcontroller.m (3.5 inches and 4-inches screen for ios7 & ios6).
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 65, 162, 57)];
imgLogo.image=[UIImage imageNamed:@"Logo-01.png"];
[self.view addSubview:imgLogo];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(8, 415, 300, 153 )];
imgBuilding.image=[UIImage imageNamed:@"image-02.png"];
imgBuilding.alpha=0.4;
[self.view addSubview:imgBuilding];
}
else
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 70, 162, 57)];
imgLogo.image=[UIImage imageNamed:@"Logo-01.png"];
[self.view addSubview:imgLogo];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 345, 320,140 )];
imgBuilding.image=[UIImage imageNamed:@"image-02.png"];
imgBuilding.alpha=0.4;
[self.view addSubview:imgBuilding];
}
Upvotes: 0
Views: 955
Reputation: 1179
You have to check whether os is IOS 7 or Earlier and depending upon that you have set your imageView.
UIImageView *imgLogo;
UIImageView *imgBuilding;
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 65, 162, 57)];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(8, 415, 300, 153 )];
}
else
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 45, 162, 57)];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(8, 395, 300, 153 )];
}
imgLogo.image=[UIImage imageNamed:@"icon.png"];
[self.view addSubview:imgLogo];
imgBuilding.image=[UIImage imageNamed:@"icon.png"];
imgBuilding.alpha=0.4;
[self.view addSubview:imgBuilding];
}
else
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 70, 162, 57)];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 345, 320,140 )];
}
else
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 50, 162, 57)];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 325, 320,140 )];
}
imgLogo.image=[UIImage imageNamed:@"icon.png"];
[self.view addSubview:imgLogo];
imgBuilding.image=[UIImage imageNamed:@"icon.png"];
imgBuilding.alpha=0.4;
[self.view addSubview:imgBuilding];
}
Hope this will help you.
Upvotes: 4
Reputation: 20234
You can do this via autoResizing
aka 'Spring and Struts' or by utilizing the latest 'Autolayout' feature.
Either way allows you to maintain a single xib file while the positioning and sizing of the subViews
remains the same (depending on the way you have set it up)
They can be set up via xib/storyboard as well as programmatically.
Springs and Struts was the previous method but it's still available:
subView
Links:
Autolayout requires you to be comfortable with the concept of constraints
It's a tad bit more effort but hey, if apple says it's better, it sure is.
Links"
NOTE: You can't use both together. You have to disable Autolayout feature on the view if you want to use Springs & Struts
Upvotes: 2