Want to use muliple nibs for different iphone interface orientations

前端 未结 4 532
自闭症患者
自闭症患者 2020-12-20 22:57

I have a situation, I have created two different nibs, one in Portrait Mode and other in Landscape mode. I had lots of designs in the view so, i had to opt for two different

相关标签:
4条回答
  • 2020-12-20 23:08

    You can't init self again once self has already been init'ed.

    When rotating, you will need to:

    1. Use loadNibNamed:owner:options: to load the nib by hand into an NSArray. (See the documention).
    2. Set self.view to the object at index 0 of that array.
    0 讨论(0)
  • 2020-12-20 23:10

    You should create a special view-controller for landscape mode, and present it modally when device rotates. To be notified when device rotates, register for UIDeviceOrientationDidChangeNotification notifications.

    For such complex views that need to be presented differently in portrait and landscape, this is recommended way of implementing it by Apple. Read here for more details:

    http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW26

    Here's a snippet from Apple's documentation. Register for notifications in your init or awakeFromNib:

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

    and in orientationChanged present your landscape view-controller modally, or dismiss it, all according to current rotation:

    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
            !isShowingLandscapeView)
        {
            [self presentModalViewController:self.landscapeViewController
                                    animated:YES];
            isShowingLandscapeView = YES;
        }
        else if (deviceOrientation == UIDeviceOrientationPortrait &&
                 isShowingLandscapeView)
        {
            [self dismissModalViewControllerAnimated:YES];
            isShowingLandscapeView = NO;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 23:10

    I needed to do the same for my own application and finally ended up with a specific UIAutoRotateView class that will handle that for you.

    see explanation here

    Hope this helps.

    0 讨论(0)
  • 2020-12-20 23:23

    shouldAutorotateToInterfaceOrientation: should simply return with a YES or a NO. In my experience, its not a good idea to do any extended processing here. From the docs:

    Your implementation of this method should simply return YES or NO based on the value in the interfaceOrientation parameter. Do not attempt to get the value of the interfaceOrientation property or check the orientation value reported by the UIDevice class. Your view controller is either capable of supporting a given orientation or it is not.

    A better place to load your nibs in response to a device rotation would be

    willRotateToInterfaceOrientation:duration: or didRotateToInterfaceOrientation:

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