微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MPMusicPlayerApplicationController 在 Swift 中的横向应用中以纵向显示

如何解决MPMusicPlayerApplicationController 在 Swift 中的横向应用中以纵向显示

我使用 Swift 开发了一款运行良好且设计为横向模式的游戏。然后我被要求为背景音乐添加 iTunes 音乐,这也很容易。但是,当媒体选择器被调用并且需要在我的应用中使用纵向视图时,我遇到了麻烦,而该视图完全是横向设计的。

我可以通过将纵向模式添加到我的 .plist 来显示 MPMediaPickerController,只要用户在启动设备时将其侧向握住,一切都很棒。但是,如果他们将其直立放置,则视图会被压缩到无法正常看到的程度,因为它们是为景观设计的。

我意识到我可以重新设计视图以在纵向上正确布局,但除此之外,我希望有一些方法可以为大多数应用强制横向,但允许媒体选择器纵向。

我在视图中尝试了以下加载功能

UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

以及媒体选择器功能中的以下内容

//MARK: - Media Picker Functions
@objc func selectSongs(_ sender: UIButton) {
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
    
    let controller = MPMediaPickerController(mediaTypes: .music)
    controller.allowsPickingMultipleItems = true
    controller.modalPresentationStyle = .popover
    controller.popoverPresentationController?.sourceView = sender
    controller.delegate = self
    present(controller,animated: true)
}
    
func mediaPicker(_ mediaPicker: MPMediaPickerController,didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    // Get the system music player.
    let musicPlayer = MPMusicPlayerController.systemMusicPlayer
    musicPlayer.setQueue(with: mediaItemCollection)
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
    mediaPicker.dismiss(animated: true)
    // Begin playback.
    musicPlayer.play()
}

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
    mediaPicker.dismiss(animated: true)
}

然后我添加了以下内容以防止用户更改设备方向时布局发生变化:

//override public var shouldAutorotate: Bool {return false}
override public var shouldAutorotate: Bool {
    return supportedInterfaceOrientations == .landscape
}

并且一切正常,只要用户在启动期间没有将设备保持直立。如果您循环到具有相同视图的第二个视图控制器确实加载了代码并返回了几次,那么它会完全更正,无论设备如何保持,大部分应用程序都处于横向状态,并且只有媒体选择器以纵向方式呈现。

我已经尝试将相同的代码添加到初始视图控制器中的各种视图覆盖以及应用程序委托文件中,但没有运气。

override func viewWillAppear(_ animated: Bool) {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeRight
}

override func viewDidAppear(_ animated: Bool) {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
}

override func viewWillLayoutSubviews() {
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
}

有没有办法让应用在设备直立时横向启动?

我还想知道我是否从错误的角度处理问题,并尝试了各种方式来呈现 MPMediaPickerController,以便它可以纵向显示而不是横向显示,因为这会导致应用程序崩溃,但没有我在下面尝试过也适用于此。

controller.modalPresentationStyle = .currentContext
controller.modalPresentationStyle = .fullScreen

我什至从以下链接添加了整个最终类 OrientationController:

How to force view controller orientation in iOS 8?

任何建议将不胜感激!

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。