2015-04-16 14 views
8

Hey Thành viên Stackoverflow, có thể bạn có thể giúp tôi khắc phục sự cố của mình.Không thể được hỗ trợInterfaceOrientationsForWindow để làm việc với Swift 1.2

Vấn đề là tôi muốn khóa hướng cho tất cả UIViewControllers thành "Chân dung" nhưng nếu MoviePlayer xuất hiện nó sẽ chuyển sang chế độ ngang và quay lại nếu trình phát phim biến mất.

Cho đến Swift 1.2 tôi đã sử dụng:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask { 
//If the video is being presented, let the user change orientation, otherwise don't. 
if let presentedViewController = window.rootViewController?.presentedViewController? { 
    if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) { 
     return .AllButUpsideDown 
    } 
} 
return .Portrait 
} 

Với Swift 1.2 một số điều thay đổi và vì vậy tôi đã kết thúc với đoạn mã sau:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    //If the video is being presented, let the user change orientation, otherwise don't. 
    if let presentedViewController = window?.rootViewController?.presentedViewController { 
     if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) { 
      return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue) 
     } 
    } 
    return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
} 

Nhưng mã của tôi không hoạt động, Movie Trình phát (XCDYoutube) bị khóa ở chế độ dọc. Hướng thiết bị nên được thiết lập tốt!

Cảm ơn trước sự giúp đỡ của bạn!

+0

Tôi có hai đề xuất nhanh: 1. Đảm bảo tệp Info.plist cho dự án của bạn không chứa khóa cho định hướng giao diện được hỗ trợ loại trừ chế độ ngang. 2. Đặt điểm ngắt tại dòng lệnh 'if' đầu tiên trong hàm supportedInterfaceOrientationsForWindow của bạn và sau đó chỉ cần thực hiện một bước thông qua mã để xem điều gì đang xảy ra. – dean

+0

hơn nữa để ý kiến ​​của deanware - bạn có chắc chắn của nó nhấn breakpoint? Bạn đang trả về một/không UIInterfaceOrientationMask. Bạn đã thử nâng cấp lên tùy chọn menu 1.2 nhanh chưa? – johndpope

+0

Tôi vừa thử một dự án mới với mã Swift 1.2 của bạn và nó hoạt động tốt. Ứng dụng chỉ quay khi MoviePlayer được trình bày. Bạn có thể có một vấn đề khác. Có lẽ XCDYoutube không chơi tốt với iOS 8 và không quay được nữa? Cố gắng quay lại Tất cả các định hướng mọi lúc để xem trình phát phim có quay hay không. – pteofil

Trả lời

3

Tôi có logic tương tự như của bạn nhưng kết thúc trả về hỗ trợ cho tất cả các định hướng.

return UIInterfaceOrientationMaskTất cả trong appdelegate.

Tùy thuộc vào số lượng bộ điều khiển xem bạn có - bạn có thể muốn tạo lớp con trừu tượng của UIViewController và chỉ trả lại hỗ trợ cho Portrait/và sau đó hack bộ điều khiển chế độ xem youtube của bạn để hỗ trợ cảnh quan.

  • (NSUInteger) được hỗ trợInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; }
1

Tôi vừa gặp vấn đề tương tự. Tôi tìm thấy một cách để sửa chữa nó bằng cách tiếp cận trên cùng của bộ điều khiển ngăn xếp:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    //If the video is being presented, let the user change orientation, otherwise don't. 
    if var presentedViewController = window?.rootViewController?.presentedViewController { 
     // Get the controller on the top of the stack 
     while (presentedViewController.presentedViewController) != nil { 
      presentedViewController = presentedViewController.presentedViewController! 
     } 

     if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) { 
      return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue) 
     } 
    } 
    return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
} 

Bạn cũng có thể thử để hiển thị các loại presentedViewController để chắc chắn đó là một trong những quyền:

println("presentedViewController type: \(presentedViewController.dynamicType)") 
Các vấn đề liên quan