2014-09-30 25 views
1

我有一個應用程序,我已啓用除了在我的項目中縱向顛倒的所有查看模式。這適用於iOS7,用於縱向和橫向模式下的視頻播放。然後,iOS 8發生了。 在我的應用程序委託:UIWebView視頻播放風景模式適用於iOS 7,而不適用於iOS 8

var fullScreenVideoIsPlaying: Bool = false 
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int { 
    if fullScreenVideoIsPlaying == false || UIApplication.sharedApplication().statusBarOrientation.isLandscape { 
     return Int(UIInterfaceOrientationMask.Portrait.toRaw()) 
    } else { 
     return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw()) 
    } 
} 
在我的視圖控制器

,我使用2個通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeView:", name:UIWindowDidBecomeVisibleNotification, object: self.view.window) 
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoPlayback:", name:UIWindowDidBecomeHiddenNotification, object: self.view.window) 

通知方法叫:

func changeView(sender:AnyObject) { 
    let string = "\(sender.object)" 
    if string.rangeOfString("MPFullscreenWindow") != nil { 
     let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 
     appDelegate.fullScreenVideoIsPlaying = false 
    } 
} 

func videoPlayback(sender:AnyObject) { 
    let string = "\(sender.object)" 
    if string.rangeOfString("MPFullscreenWindow") != nil { 
     let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 
     appDelegate.fullScreenVideoIsPlaying = true 
    } 
} 

我webView的...我玩我的視頻在webview中,而不是我的選擇,我不能改變這個

func setupVideoWebView() { 
    let request = NSURLRequest(URL: NSURL(string: "https://www.youtube.com/watch?v=3U7xtCBmieQ")) 
    videoWebView.loadRequest(request) 

    videoWebView.scrollView.scrollEnabled = false 
    videoWebView.scrollView.bounces  = false 
    videoWebView.delegate     = self   

    self.view.addSubview(videoWebView) 
} 

這適用於iOS 7,我知道這是危險的B/C我正在尋找一個可以改變的字符串。那麼,它的確適用於iOS 8.在iOS 8中,我在旋轉應用程序後在控制檯中看到了這一點:

無法同時滿足約束條件。 以下列表中的至少一個約束可能是您不想要的約束之一。試試這個:(1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints) ( 「」, 「= 10) - 的UIImageView:0x7fa964a8c9b0](名稱: '|': AVAudioOnlyIndi​​catorView:0x7fa964ac8550)>「, 」「,

它會持續一段時間。它讓我認爲iOS不喜歡我的肖像/風景限制。由於我沒有直接使用MPMoviePlayerViewController,因此我無法使用像this:這樣的幫助。我已經嘗試添加約束

func webViewDidFinishLoad(webView: UIWebView) 

但沒有運氣。 Autolayout是我最好的選擇嗎?我一直把它看作是對其他類似問題的建議,但無法爲此達到目的。任何幫助是極大的讚賞。

回答

3

我有同樣的問題,我剛剛找到解決辦法。

  1. 在UIWindow類型的應用程序委託中創建一個屬性,我稱它爲「youTube」。

  2. 添加該代碼的UIViewController包含一個UIWebView:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(youTubeWindowOpened:) 
                 name:UIWindowDidBecomeVisibleNotification 
                 object:self.view.window 
    ]; 
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(youTubeWindowClosed:) 
                 name:UIWindowDidBecomeHiddenNotification 
                 object:self.view.window 
    ]; 
    
  3. 然後,寫這些新的方法有兩種:最後

    - (void)youTubeWindowOpened:(NSNotification *)sender 
    { 
        appDelegate.youTube = (UIWindow *)sender.object; 
    } 
    
    - (void)youTubeWindowClosed:(NSNotification *)sender 
    { 
        appDelegate.youTube = nil; 
    } 
    
  4. ,修改 「 - 應用程序:supportedInterfaceOrientationsForWindow:」在AppDelegate中:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
    { 
        NSString *nom = NSStringFromClass([[[UIApplication sharedApplication] keyWindow] class]); 
    
        // iOS 8 
        if (youTube && youTube == window) { 
         return UIInterfaceOrientationMaskLandscapeRight; 
        } 
    
        // iOS < 8 
        else if ([nom isEqualToString:@"MPFullscreenWindow"]) { 
         return UIInterfaceOrientationMaskLandscapeRight; 
        } 
        else { 
         return UIInterfaceOrientationMaskPortrait; 
        } 
    } 
    

我知道這不是一個完美的解決方案,但我希望它有幫助!

相關問題