如何在xcode中更改设备方向时加载xib?

问题描述:

当设备的方向改变时,我将如何加载新的xib?即当装置转向横向时加载横向的xib,反之亦然。我将如何去自动检测方向变化并从那里调整?如何在xcode中更改设备方向时加载xib?

显然是所有我需要做的就是创建一个视图基于应用程序反过来shouldAutoRotateToInterfaceOrientaion为YES,并创建一个单独的厦门国际银行文件的两个UIViewController的子类,然后将其添加到应用程序的视图控制器:

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

,并添加此方法进入应用程序视图控制器:

-(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]; 
    } 

} 

这就是它,当设备旋转时,xib自动加载的方向是正确的。您还需要以编程方式添加两个viewcontrollers(@property和@synthesize它们),以及IB中appviewcontroller.xib中的两个viewcontrollers,每个类的名称都与您创建的每个xib的viewcontroller子类相对应。然后添加一个视图将其连接到文件的所有者,并将IB中的插座连接到您在ib中创建的视图控制器,并且应该全部为

+0

这对iPhone非常适用。 iPad需要额外的步骤:#define DegreesToRadians(x)((x)* M_PI/180.0); (self.view.transform = CGAffineTransformMakeRotation(DegreesToRadians(180))至少在我的情况下。 – TrekOnTV2017 2011-12-05 19:01:01

+0

+1为我工作..谢谢.. :) – HDdeveloper 2013-01-23 15:06:13