Iphone 如何阻止mainView旋转?
我有这个代码,设置为当手机旋转到横向时显示时钟视图,然后当手机返回到纵向时返回主视图。但当它返回到纵向时,纵向视图处于横向模式。我该如何解决这个问题?这是我的密码Iphone 如何阻止mainView旋转?,iphone,objective-c,cocoa-touch,orientation,Iphone,Objective C,Cocoa Touch,Orientation,我有这个代码,设置为当手机旋转到横向时显示时钟视图,然后当手机返回到纵向时返回主视图。但当它返回到纵向时,纵向视图处于横向模式。我该如何解决这个问题?这是我的密码 -(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { UIInterfaceOrientation toOrientat
-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if(toOrientation == UIInterfaceOrientationPortrait)
{
self.view = mainView;
}
else if(toOrientation == UIInterfaceOrientationLandscapeLeft)
{
self.view = clockView;
}
else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = mainView;
}
else if(toOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = clockView;
}
}
最好将mainView和clockView作为视图控制器的子视图:
// in viewDidLoad do [self.view addSubView:mainView]; [self.view addSubView:clockView]; clockView.hidden = YES;
-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsPortrait(toOrientation)) {
mainView.hidden = NO;
clockView.hidden = YES;
}
else {
mainView.hidden = YES;
clockView.hidden = NO;
}
}
这样,两个视图都会以正确的方式自动旋转,您的问题应该会消失