0

I am trying to create my iOS application completely programmatic without using storyboard. Now I am struggling for different device orientation Identification. I did separate UI sizes by using device height but I need to create for landscape orientation different sizes, For me by using below code I cant handle device orientation.

My Code Below :

   // Device difrrentiate
    if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && ((UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)))) {

        // The device is an iPhone or iPod touch.
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if (result.height == 480) {

    // Here I can declare the UI sizes for iPhone 3G & 3GS & 4 & 4S
        }
        if (result.height == 568) {

    // Here I can declare the UI sizes for iPhone 5, 5C & 5S
        }
        if (result.height == 667) {

    // Here I can declare the UI sizes for iPhone 6
        }
        if (result.height == 736) {

           // Here I can declare the UI sizes for iPhone 6 Plus
        }
       }
       } else if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) && ((UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)))) {

        // The device is an iPad and iPad mini running iOS 3.2 or later.
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if (result.height == 1024) {

        // Here I can declare the UI sizes for iPad, iPad 2, iPad Mini
        }
        if (result.height == 2048) {

         // Here I can declare the UI sizes for iPad Air, iPad Mini Retina
        }
     }
 }
Vvk
  • 4,031
  • 29
  • 51
SteaveJobs
  • 33
  • 2
  • 8
  • 1
    Possible duplicate of [How to change the device orientation programmatically in iOS 6](http://stackoverflow.com/questions/12650137/how-to-change-the-device-orientation-programmatically-in-ios-6) – Badal Shah Feb 09 '16 at 04:46
  • without giving clear answer dont put duplicate possiblilty. Before post my question I tried everything still I am not solviong my problem. Please understand to remove duplicate from my post @Badal Shah – SteaveJobs Feb 09 '16 at 05:05
  • you can find your answer in this Answer on that link. Try http://stackoverflow.com/a/12651309/4910767. – Badal Shah Feb 09 '16 at 05:10
  • I will give you the solution. – user3182143 Feb 09 '16 at 05:40

1 Answers1

0

For landscape this should work:

if(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation==UIInterfaceOrientationLandscapeRight)

Check this:

    @interface ViewController ()
    {
        UIButton *btn;
        UIView *vw;
    }
    @end

    @implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor whiteColor];
    btn=[UIButton buttonWithType:UIButtonTypeCustom];
    vw=[[UIView alloc]init];

    btn.frame=CGRectMake(100, 100, 60, 40);
    vw.frame=CGRectMake(100, 360, 150, 100);

    btn.backgroundColor=[UIColor redColor];
    vw.backgroundColor=[UIColor underPageBackgroundColor];
    [self.view addSubview:btn];
    [self.view addSubview:vw];
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {
        //in landscape width=480 height=300. statusbar Height=20 by default
        btn.frame=CGRectMake(150, 60, 60, 40);
        vw.frame=CGRectMake(150, 200, 150, 100);
    }
    else
    {
        //in portrait width=320 height=460. statusbar Height=20 by default
        btn.frame=CGRectMake(100, 100, 60, 40);
        vw.frame=CGRectMake(100, 360, 150, 100);
    }
}

Hope this helps.

Satish A
  • 584
  • 4
  • 17