How to load xib when changing device orientation in xcode?

How can I load the new xib when changing the orientation of the device? those. load landscape-oriented xib when the device turns into landscape orientation and vice versa with a portrait. and how can I automatically detect a change in orientation and adjust from there?

+6
objective-c cocoa-touch
source share
1 answer

Apparently all I have to do is create a view-based application, toAutoRotateToInterfaceOrientaion for YES, and create two subclasses of uiviewcontroller with separate xib files, and then add this to the application view controller:

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

and add this method to the application view controller:

 -(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 this, when the device is rotated, xib automatically boots in the correct orientation.

edit: you also need to programmatically add two view managers (@property and @synthesize), as well as two view managers in appviewcontroller.xib in IB, each of which has a class name corresponding to a subclass of viewcontroller of each xib you create. then add a view, connect it to the file owner and connect the sockets in IB to the view controllers that you created in ib, and that should be all

+7
source share

All Articles