2013-01-04 29 views
5

Trong ứng dụng toàn cầu của tôi, tôi cần xử lý các hướng khác nhau cho iphone và ipad. Đối với ipad tôi cần phải cho phép định hướng phong cảnh và cho iphone chân dung một mình. Tôi đã trả lại mã bên dưới tại sốVấn đề định hướng iOS 6 trong ứng dụng toàn cầu

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
     return UIInterfaceOrientationIsPortrait(interfaceOrientation); 

    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
} 

đang hoạt động tốt nhất trong IOS 5 Nhưng trong phương pháp tự động không chạy iOS 6. Sau đó tôi đã thay đổi phương pháp để,

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

thậm chí phương pháp này không được ở tất cả các bị sa thải trong IOS 6.

thiết lập plist của tôi là

enter image description here

tôi cần phải xử lý cả hai định hướng [iPhone-chân dung, iPad-phong cảnh] cho cả iOS 5 và IOS 6. Vui lòng hướng dẫn tôi khắc phục vấn đề này.

+0

xin vui lòng xem Vì vậy, câu trả lời của tôi http: // stackoverflow.com/questions/12933089/i-muốn-to-make-my-appliaction-only-in-landscape-direction-in-ios-cả-ios-5-a – Deepesh

+0

Ngoài câu trả lời được chấp nhận, 'preferredInterfaceOrientationForPresentation' yêu cầu một hướng duy nhất và bạn đang trả về một mặt nạ ('UIInterfaceOrientationMaskPortrait') thay vì ví dụ 'UIInterfaceOrientationPortrait'. – Jesper

Trả lời

3

Trình quản lý rootview của bạn là bộ điều khiển UINavigation hoặc UITabbarcontroller ?? Nếu các phương thức này không hoạt động nếu bạn đang gọi các phương thức này trong bộ điều khiển khung nhìn của bạn.

Vì vậy, hãy tạo danh mục C mục tiêu trên các bộ điều khiển chế độ xem vùng chứa này và thêm vào dự án của bạn.

@implementation UINavigationController (autorotate) 


-(NSUInteger)supportedInterfaceOrientations 
{ 
    //make the check for iphone/ipad here 

if(IPHONE) 
    { 
return UIInterfaceOrientationMaskPortrait; 
    } 
else 
    { 
return UIInterfaceOrientationMaskLandscape; 
    } 

} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
return UIInterfaceOrientationPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
return NO; 
} 
0

gì bạn muốn là như sau:

#pragma mark - iOS 5.1 and under Rotation Methods 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return UIInterfaceOrientationIsPortrait(interfaceOrientation); 
    } 
    else{ 
    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
    } 
} 

#pragma mark - iOS 6.0 and up Rotation Methods 

- (BOOL)shouldAutorotate; { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); 
    } 
    else{ 
    return UIInterfaceOrientationMaskLandscape; 
    } 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; { 
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ 
    return UIInterfaceOrientationPortrait; 
    } 
    else{ 
    return UIInterfaceOrientationLandscapeLeft; 
    } 
} 

Những gì bạn phải chắc chắn của là 2 lần.

Đầu tiên, điều này được triển khai trong bạn RootViewController. Nếu bạn đang nhúng UIViewController vào bên trong một UINavigationController (hoặc UITabBarController), thì bạn phải tạo một lớp tùy chỉnh UINavigationController (hoặc một lớp tùy chỉnh UITabBarController) thực hiện các phương pháp này và sử dụng các phương pháp đó.

Thứ hai, bạn phải đảm bảo rằng bạn đã định hướng được hỗ trợ trong tệp Info.plist. Nếu không, hệ thống sẽ ghi đè các giá trị trả về của các phương thức này (Lưu ý: bạn cũng có thể dễ dàng thay đổi các giá trị này bằng cách nhấp vào các nút trong trang đích của bạn cũng như trang Tóm tắt).

Hy vọng rằng Trợ giúp!

2

thêm phương pháp này để ứng dụng của bạn ủy của nó 100% làm việc

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return UIInterfaceOrientationPortraitUpsideDown; 
    } 
    else 
     return UIInterfaceOrientationMaskAll; 
} 
+0

đơn giản và hoạt động hoàn hảo! –

0

Dưới đây là phiên bản nhanh chóng của câu trả lời Pradeep của. Bạn không cần phải phân lớp của tất cả các bộ điều khiển điều hướng của bạn để có được những gì bạn muốn làm việc (aka, tắt định hướng phong cảnh cho iPhone chỉ)

Chỉ cần thêm video này vào ứng dụng đại biểu của bạn:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
    } else { 
     return Int(UIInterfaceOrientationMask.All.rawValue) 
    } 
} 
Các vấn đề liên quan