Landscape xib appears as portrait

I have a view controller and separate nib files for portrait and landscape. On rotating, I load the respective nib. The methods

 shouldAutorotateToInterfaceOrientation and willRotateToInterfaceOrientation

get called and the nib does change.

  • App running slow after loading viewcontroller, then unload about 15-20 times
  • Rotate image in Quartz? Image is upside down! (iPhone)
  • Handling autorotation for one view controller in iOS7
  • How to inform the parent viewcontroller about the changed screen orientation in the modal view controller?
  • Trouble translating Touch coordinates following device orientation change
  • UICollectionView - resizing cells on device rotate - Swift
  • The problem:

    the landscape nib does not appear as landscape, but portrait! The status bar is
    correctly rotated and appears on the top:
    (Sorry, couldn't paste the image, because my account is new. The screenshot is in   
    landscape, with a landscape status bar, but a landscape view shown as portrait.)
    

    One would think the problem lies in not setting the orientation as Landscape in IB Simulated metrics for the view, but I’ve done that and the view appears as landscape in IB. (I don’t think it would even be possible to create a rotated button like that if the view was portrait.) Besides these two nibs I have a mainwindow.xib, which contains the app delegate, window and view controller objects.

    EDIT: I realized that the views are actually rotating, when they should “stay up”. It’s like there’s an extra transformation. When I rotate the phone 90° right, the landscape xib is displayed rotated 90° right. When I rotate the phone another 90° right, the portrait xib is displayed upside down. The status bar is always correctly displayed at the top.

    EDIT2: Using

    self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
    

    in willRotateToInterfaceOrientation I can rotate the view to landscape left (and to any orientation I want), so I can use that as a workaround. However, I have other projects, where the view rotates automatically and doesn’t require the use of CGAffineTransformMakeRotation. It’s like something is preventing the automatic rotation here.

    2 Solutions Collect From Internet About “Landscape xib appears as portrait”

    Are you adding the view loaded from nib as subView? If Only the status bar is rotating it means your previous view is hung while releasing the view and adding the new view.Can you tell how are you adding the view loaded from xib to the SuperView.

    Make sure you are releasing the previous view correctly while loading the other view,put NSLOG in dealloc of the views and check whether the view is getting released completely.

    I had done something similar to this only instead of making an nib file separately I just added two subviews to the main nib as prtraitView and Landscape View

    and switched them as follows

    In viewDidAppear method

    -(void)viewDidAppear:(BOOL)animated{
    
    
    
    
        if(UIDeviceOrientationIsPortrait(self.interfaceOrientation))
        {
            self.portraitVIew.frame=self.view.bounds;
            self.portraitVIew.frame=self.view.frame;
            [self.view addSubview:self.portraitVIew];
        }else{
            self.landscapeView.frame=self.view.frame;
            [self.view addSubview:self.landscapeView];
        }
        self.view.autoresizesSubviews = YES;
    
    
    
    
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(deviceOrientationDidChangeNotification:)
         name:UIDeviceOrientationDidChangeNotification
         object:nil];
    
    
    }
    

    and then Implemented deviceOrientationDidChangeNotification as follows

    - (void)deviceOrientationDidChangeNotification:(NSNotification*)note
    {
    
        if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
    
        }else{
            UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
            if(UIDeviceOrientationIsLandscape(self.interfaceOrientation))
            {
                self.landscapeView.hidden=NO;
                self.landscapeView.frame=self.view.frame;
                [self.portraitVIew removeFromSuperview];
    
    
                [self.view addSubview:self.landscapeView];
            }else {
                self.landscapeView.hidden=YES;
                self.portraitVIew.frame=self.view.frame;
                NSLog(@"Portrait");
    
                [self.view addSubview:self.portraitVIew];
            }
        }
    
    
    
    }
    

    It worked very well for me..