【问题标题】:UIWebView video playback landscape mode works in iOS 7, not iOS 8UIWebView 视频播放横向模式适用于 iOS 7,而不是 iOS 8
【发布时间】:2014-11-24 18:06:37
【问题描述】:

我有一个应用程序,我在项目中启用了除纵向倒置之外的所有查看模式。这适用于 iOS7 以纵向和横向模式播放视频。然后,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)

但没有运气。自动布局是我最好的选择吗?我一直将其视为对其他类似问题的建议,但无法让它为此目的工作。非常感谢任何帮助。

【问题讨论】:

    标签: ios uiwebview ios8 mpmovieplayer


    【解决方案1】:

    我有同样的问题,我刚刚找到了解决方法。

    1. 在应用程序委托中创建一个 UIWindow 类型的属性,我称之为:“youTube”。

    2. 将此代码添加到包含 UIWebView 的 UIViewController:

      [[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. 最后,在 AppDelegate 中修改你的“-application:supportedInterfaceOrientationsForWindow:”:

      - (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;
          }
      }
      

    我知道这不是一个完美的解决方案,但我希望它有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 2014-11-16
      • 2014-10-02
      • 1970-01-01
      • 2022-11-01
      • 2019-01-27
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多