我有一个视图控制器(带有子视图控制器),它被锁定在纵向,使用以下代码来完成此操作:
- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.centerViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAll;
}
这非常有效,如果需要锁定视图,我可以“覆盖”当前centerViewController中的supportedInterfaceOrientations,如果希望视图支持所有内容,则可以省略它。
问题是,被锁定的视图在导航时不会旋转到其支持的方向。一个视图锁定在纵向视图中,但当在横向视图中显示另一个视图并导航到此视图时,即使此视图不支持横向视图,也会在横向视图中显示该视图。
如何确保视图在导航时旋转到允许的方向?
发布于 2016-01-07 12:43:07
如果您在supportedInterfaceOrientations
中返回了受支持的方向,我将自动旋转。
#pragma mark - Orientation Handling
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate {// iOS 6 autorotation fix
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {// iOS 6 autorotation fix
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {// iOS 6 autorotation fix
return UIInterfaceOrientationPortrait;
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
}
}
发布于 2016-01-07 12:58:08
在AppDelegate.h
中添加以下属性
@property (readwrite) BOOL restrictRotation;
在AppDelegate.m
中添加以下代码
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait;
}
在要限制方向的ViewController.m中添加以下代码:
-(BOOL)shouldAutorotate
{
return NO;
}
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
- (NSUInteger) supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskLandscape;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// (iOS 6)
// Prefer (force) landscape
return UIInterfaceOrientationLandscapeRight;
}
同样,当你离开你的Landscape ViewController.m时,进行下面的函数调用
[self restrictRotation:NO];
希望我能解决你的问题。
https://stackoverflow.com/questions/34655338
复制相似问题