Landscape xib appears as portrait

前端 未结 2 1273
走了就别回头了
走了就别回头了 2021-02-14 15:38

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

 shouldAutorotateToInterfaceOrie         


        
相关标签:
2条回答
  • 2021-02-14 16:04

    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..

    0 讨论(0)
  • 2021-02-14 16:07

    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.

    0 讨论(0)
提交回复
热议问题