[英]Handling Orientation in Ios7 for specific ViewControllers
我正在創建一個電子書應用程序,在該應用程序中,我僅將所有方向都賦予了兩個ViewController。
我很想成功地將方向設置為兩個ViewController並第一次執行Rotation。但是后來當我用新書加載View Controller時,旋轉無法正常工作。
我用的方法:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
//code for landscape
}
else{
//code for protrait.
}
}
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return (UIInterfaceOrientationMaskAll);
}
嘗試處理UINavigationControllers上的旋轉,UINavigationController子類上的確切代碼是正確的。 並確保將UIViewControllers設置為新UINavigationControllers的childViewControllers。 請記住,您需要在info.plist上使用鍵“支持的界面方向”來設置支持的方向。
創建UINavigationController的子類
@interface BaseNavigationController:UINavigationController
@結束
@implementation BaseNavigationController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
BOOL shouldAutorotateBool = NO;
if ([self isRotate]) {
if([[self.viewControllers lastObject] respondsToSelector:@selector(shouldAutorotate)])
shouldAutorotateBool = [[self.viewControllers lastObject] shouldAutorotate];
}
return shouldAutorotateBool;
}
// iOS6/7 support
- (NSUInteger) supportedInterfaceOrientations {
NSUInteger interfaceOrientation = UIInterfaceOrientationMaskPortrait;
if ([self isRotate]) {
if([[self.viewControllers lastObject] respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
interfaceOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
}
return interfaceOrientation;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationPortrait;
if ([self isRotate]) {
if([[self.viewControllers lastObject] respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
interfaceOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
}
return interfaceOrientation;
}
-(BOOL)shouldAutorotate
{
BOOL shouldAutorotateBool = NO;
if ([self isRotate]) {
if([[self.viewControllers lastObject] respondsToSelector:@selector(shouldAutorotate)])
shouldAutorotateBool = [[self.viewControllers lastObject] shouldAutorotate];
}
return shouldAutorotateBool;
}
-(BOOL) isRotate
{
if ([[self.viewControllers lastObject] isMemberOfClass:NSClassFromString(@"VideoVC")]) {
return YES;
}
return NO;
}
@end
在那個特定的viewController中:
#import "VideoVC.h"
@interface VideoVC ()
@end
@implementation VideoVC
#pragma mark- Methods for device orientation handling
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (BOOL)shouldAutorotate
{
return YES;
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.