View Orientation changes

 ̄綄美尐妖づ 提交于 2019-12-12 05:14:22

问题


Update: I found why it was loading the same view the whole time. I now fixed that. It was a typo

ControllerForSplitViews.m

- (id)init
{
    self = [super initWithNibName:@"ControllerForSplitViews" bundle:nil];
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
        self.view = landscapeView;
    }
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        self.view = portraintView;
    }
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    return self;
}

This works.

Now the problem. When the view is loaded and running I change the orientation and load a different view from the same nib file, Fixed: but as soon as I do this the other UI elements disappears and only the ui elements of my landscape view is kept and when I rotate the device again it doesn't load my portrait view again? Any suggestions why this might happen?

-(void) orientationChanged:(id)object
{   
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        self.view = portraintView;
    }
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        self.view = landscapeView;
    }
 }

New Problem, each time I change the orientation it loads the opposite orientation's view

Thanks


回答1:


I am adding this here as well for incase someone ever reads this, they might want to check out this post here where a simular scenario was discussed

Two views Multiple UIPickerViews Single outlet




回答2:


Ok so I found a fix that works, but I have no idea why my original code didn't work what I did was:

Remove:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

Remove:

-(void) orientationChanged:(id)object
{   
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        self.view = portraintView;
    }
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        self.view = landscapeView;
    }
 }

Change:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        self.view = landscapeView;

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
          (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        self.view = portraintView;

    }
    return YES;
}

And now it works perfectly



来源:https://stackoverflow.com/questions/6689325/view-orientation-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!