0

How can i modify my code so same app can run on iphone with same UI elements. I made changes in info Plist and made it universal but Problem is that i have not used stroryboard or nib , xib files to display UI elements. also i hardcoded the height and width for particular button or label ,frame etc.

So is there any way to auto resize screen to ipad view to iphone Or I need to write every code with new height and width for iPhone?

like in android i detect the height and width and according to that i divide it by 2 or 3 to place particular UI item on different screen size.

Also how can i detect that app is running on iphone 4 or iphone 5 or iPad so i can change my images and value or height n width?

  • 1
    Well, you basically shot yourself in the foot by hard-coding your UI stuff - if you have had nibs or storyboards, chances are you wouldn't even need to change any code at all. – Jeff Feb 21 '13 at 13:42
  • that means i dont have any choice to start new project with storyboard support right? –  Feb 21 '13 at 13:53

3 Answers3

1

You can get the screen bounds using:

[[UIScreen mainScreen] bounds];

The quantity of changes that you would have to make depends on how you designed your code before, if you were using absolute values all the times you'll have much more problems than if you were using relative values...

apascual
  • 2,970
  • 1
  • 20
  • 32
  • autoresizingmask is not working in my case... do you know any method which can take device screen size run time and set it as height and width on run time like this "initWithFrame:CGRectMake(0, 0, getdevicewidth() , getdeviceheight())]" and than i can use that height and width to set every UI element like lebel or Textview accodring to device screen size automatically –  Feb 21 '13 at 14:36
  • "initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height]" – apascual Feb 21 '13 at 14:40
  • I added that But it takes value of Portrait screen not from landscape –  Feb 21 '13 at 14:48
  • It probably depends in where are you checking it... All the views are suposed to be created in portrait and then rotated into landscape... Check "viewDidAppear" – apascual Feb 21 '13 at 14:52
  • any suggestion to avoid portrait mode .. i made changes in plist to display in landscape mode but still i get portrait values –  Feb 21 '13 at 14:59
  • Where are you performing the resize? Try it in "viewDidAppear" – apascual Feb 21 '13 at 15:02
  • viewdidload all initlizing and assigning values in viewdidLoad –  Feb 21 '13 at 15:30
  • I insist... You won't have the correct bounds until "viewDidAppear" is called... Implement it there – apascual Feb 21 '13 at 15:32
  • No luck i put all nessacary code in view Did appear but still i get portrait mode –  Feb 21 '13 at 15:45
  • Mmm, but you are getting just the incorrect bound values or also your view is not rotating? – apascual Feb 21 '13 at 15:47
1

you can take a look at the UIViewAutoresizingMask property of your views, for example:

 view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

as to how to check if you are on iPhone 5 you can use this line of code :

BOOL isIPhone5 = [[UIScreen mainScreen ] bounds ].size.height == 568  ? YES : NO ;
ahmad
  • 1,212
  • 1
  • 14
  • 28
  • autoresizingmask is not working in my case... do you know any method which can take device screen size run time and set it as height and width on run time like this "initWithFrame:CGRectMake(0, 0, getdevicewidth() , getdeviceheight())]" and than i can use that height and width to set every UI element like lebel or Textview accodring to device screen size automatically –  Feb 21 '13 at 14:36
  • there's no method in the SDK as far as i know but you can create your own method in a Utility class i.e +(float)getDeviceHeight{ return [[UIScreen mainScreen ] bounds ].size.height; } – ahmad Feb 24 '13 at 07:23
0

Create seperate nibs for iphone and ipad

well doing it all codewise then you have to set the frame for all the components code wise checking the device is ipad or iphone.

Checking iPad or iPhone look here

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • "... i have not used storyboard or nib files to display UI elements. also i hardcoded the hight and width for particular button or label, etc." – Manuel Feb 21 '13 at 13:42
  • as i said I have not used any nibs and i dont want to use because my app is all graphics and animation and if i want to create nibs files for iphone and ipad that meanse i have to start new project from start...so is there any other solution?..or how can i check in every file that device is iphone or ipad? –  Feb 21 '13 at 13:44
  • i need to check by this UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad Every time when i access that class or UI element class? right? –  Feb 21 '13 at 14:04