0

Currently my App looks like this in portrait mode: enter image description here

but when my iPad is in landscape mode I want my Icons look like this: enter image description here

By now it looks like that in landscape mode:

enter image description here

I know how to rotate the icons but I don't know where I have to paste the code. I just want to look my app in landscape mode like the 2nd pic.

EDIT

It should look like this one: enter image description here

mafioso
  • 1,630
  • 4
  • 25
  • 45

4 Answers4

3

you need to set observer using NSNotificationCenter following code

NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)

in AppDelegate didFinishedLaunching method.

And in observer method you check for orientation

if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{            
    print("landscape")
}

if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
    print("Portrait")
}
RBN
  • 482
  • 4
  • 13
  • Also you can do this code in any UIViewController viewDidLoad method and add observer for same. – RBN Aug 04 '16 at 06:27
1

You need to rotate your yourimageview by 90 degree in landscape mode -

In AppDelegate.swift inside the "didFinishLaunchingWithOptions" function I put:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)

and then inside the AppDelegate class I put the following function:

     func rotated()
        {
            if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
            {            
                print("landscape")
             //Rotate 90 degrees clockwise:        
               yourimageview1.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))         
            //Rotate 90 degrees counterclockwise:        
             yourimageview2.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
            }
            if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
            {
                print("Portrait")
            }

        }

Hope this helps anyone else!

Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
1

You should manage it from Assets if your deployment target is equal or greater than 8.0. You can set different images for different size class. For example for Portrait mode in iphone you can set assset forCompact width Regular Height size class and for iphones in landscape you can set asset for Any width Compact Height size class.

Refer apple documentation for Customizing Assets for Size Classes

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Is there a way to override the rotation. My app rotates automatically and then it looks like the 3rd picture I posted and not like the 2nd – mafioso Aug 04 '16 at 06:55
  • If you set different picture for landscape and portrait then it will automatically take it for respective orientation. You not need to do anything programmatically. – Ketan Parmar Aug 04 '16 at 07:04
  • Why a different picture?! Above I posted the picture how it actually looks like and what I want it to look like – mafioso Aug 04 '16 at 07:06
  • Second thing it should be done by auto layout.If you set proper constraints then it will automatically adjust in landscape and portrait. – Ketan Parmar Aug 04 '16 at 07:12
  • And how could I set the constraints so they work for landscape and portrait mode? In portrait the icons height is view.height/2 and in landscape too as you can see on the 3rd picture. How can I solve that? – mafioso Aug 04 '16 at 07:14
  • look I've edited my post so you could probably understand it better – mafioso Aug 04 '16 at 07:18
  • Other answers have already mentioned that how to handle rotations so go likewise! – Ketan Parmar Aug 04 '16 at 07:22
0

Try as following in your ViewController.m

-(void)viewDidLoad
{
   NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
}


- (void) deviceOrientationChnaged:(NSNotification *) notification
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {            
        print("landscape")
       yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

      //OR Rotate 90 degrees counterclockwise:

       yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))

    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        print("Portrait")
    }
}

Hope this will help.

EDIT:

Please check my this question: LINK

Community
  • 1
  • 1
Rohan
  • 2,939
  • 5
  • 36
  • 65
  • I've edited my post for better understanding. Maybe you know what should be done? – mafioso Aug 04 '16 at 07:20
  • @mafioso I think you are talking about supporting your app for Landscape mode. – Rohan Aug 04 '16 at 07:29
  • It is supported in landscape mode but the icons look like the 3rd picture and I want them to look like the 4th picture – mafioso Aug 04 '16 at 07:33
  • In your answer the icons look like I want but the view isn't in landscape mode. How can I combine that? – mafioso Aug 04 '16 at 07:35