Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would i load a xib when device orientation is changed in xcode?

how would i go about loading a new xib when the orientation of the device is changed? i.e. load a landscape oriented xib when the device is turned into landscape orientation and vice-versa with portrait. and how would i go about automatically detecting an orientation change and adjusting from there?

like image 247
Joe Avatar asked Aug 29 '10 19:08

Joe


1 Answers

apparently all i have to do is create a view based app turn shouldAutoRotateToInterfaceOrientaion to YES, and create two uiviewcontroller subclasses with individual xib files, then add this to the app view controller:

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

and add this method into the app view controller as well:

-(void)orientationChanged {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
    }

}

and thats it, when the device is rotated the xib auto loads in the correct orientation.

edit:you also need to add two viewcontrollers programmatically(@property and @synthesize them also), and two viewcontrollers in the appviewcontroller.xib in IB each with the class name corresponding to the viewcontroller subclass of each xib you created. then add a view connect it to the file's owner and connect the outlets in IB to the view controllers you created in ib and that should be all

like image 114
Joe Avatar answered Nov 12 '22 05:11

Joe