在iOS 13中,模态视图控制器在呈现时有一个新的行为。
现在它不是全屏默认情况下,当我向下滑动时,应用会自动解除视图控制器。
我怎么能防止这种行为,并回到旧的全屏模态vc?
谢谢
在iOS 13中,模态视图控制器在呈现时有一个新的行为。
现在它不是全屏默认情况下,当我向下滑动时,应用会自动解除视图控制器。
我怎么能防止这种行为,并回到旧的全屏模态vc?
谢谢
当前回答
let Obj = MtViewController()
Obj.modalPresentationStyle = .overFullScreen
self.present(Obj, animated: true, completion: nil)
//如果你想禁用滑动来消除它,添加行
Obj.isModalInPresentation = true
查看苹果文档获取更多信息。
其他回答
我在ios 13中使用了swizzling
import Foundation
import UIKit
private func _swizzling(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
if let originalMethod = class_getInstanceMethod(forClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
extension UIViewController {
static let preventPageSheetPresentation: Void = {
if #available(iOS 13, *) {
_swizzling(forClass: UIViewController.self,
originalSelector: #selector(present(_: animated: completion:)),
swizzledSelector: #selector(_swizzledPresent(_: animated: completion:)))
}
}()
@available(iOS 13.0, *)
@objc private func _swizzledPresent(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
if viewControllerToPresent.modalPresentationStyle == .pageSheet
|| viewControllerToPresent.modalPresentationStyle == .automatic {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
_swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
}
}
然后放这个
UIViewController.preventPageSheetPresentation
的某个地方
例如在AppDelegate中
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UIViewController.preventPageSheetPresentation
// ...
return true
}
我添加的信息可能对某人有用。如果你有任何故事板segue,要回到旧的风格,你需要将kind属性设置为modal Present,并将Presentation属性设置为全屏。
另一种方法是在你的应用程序中拥有你自己的基本视图控制器组件,并通过一个基本的设置来实现指定的和必需的初始化器,如下所示:
class MyBaseViewController: UIViewController {
//MARK: Initialisers
/// Alternative initializer which allows you to set the modal presentation syle
/// - Parameter modalStyle: the presentation style to be used
init(with modalStyle:UIModalPresentationStyle) {
super.init(nibName: nil, bundle: nil)
self.setup(modalStyle: modalStyle)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// default modal presentation style as fullscreen
self.setup(modalStyle: .fullScreen)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
// default modal presentation style as fullscreen
self.setup(modalStyle: .fullScreen)
}
//MARK: Private
/// Setup the view
///
/// - Parameter modalStyle: indicates which modal presentation style to be used
/// - Parameter modalPresentation: default true, it prevent modally presented view to be dismissible with the default swipe gesture
private func setup(modalStyle:UIModalPresentationStyle, modalPresentation:Bool = true){
if #available(iOS 13, *) {
self.modalPresentationStyle = modalStyle
self.isModalInPresentation = modalPresentation
}
}
注意:如果你的视图控制器包含在一个以模态方式呈现的导航控制器中,那么导航控制器应该以同样的方式处理这个问题(也就是说,用同样的方式定制你的自定义导航控制器组件)
在Xcode 11.1、iOS 13.1和iOS 12.4上测试
希望能有所帮助
有多种方法可以做到这一点,我认为每一种方法都适合一个项目,但不适合另一个项目,所以我想我将它们保留在这里,也许其他人会处理不同的情况。
1-覆盖当前
如果你有一个BaseViewController,你可以重写present(_ viewcontrollertoppresent: animated flag: completion:)方法。
class BaseViewController: UIViewController {
// ....
override func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
// ....
}
使用这种方法,您不需要对任何present调用做任何更改,因为我们只是覆盖了present方法。
2-扩展:
extension UIViewController {
func presentInFullScreen(_ viewController: UIViewController,
animated: Bool,
completion: (() -> Void)? = nil) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: animated, completion: completion)
}
}
用法:
presentInFullScreen(viewController, animated: true)
3-对于一个UIViewController
let viewController = UIViewController()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true, completion: nil)
4-从故事板
选择一个segue并将演示设置为FullScreen。
5 -狂饮
extension UIViewController {
static func swizzlePresent() {
let orginalSelector = #selector(present(_: animated: completion:))
let swizzledSelector = #selector(swizzledPresent)
guard let orginalMethod = class_getInstanceMethod(self, orginalSelector), let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else{return}
let didAddMethod = class_addMethod(self,
orginalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self,
swizzledSelector,
method_getImplementation(orginalMethod),
method_getTypeEncoding(orginalMethod))
} else {
method_exchangeImplementations(orginalMethod, swizzledMethod)
}
}
@objc
private func swizzledPresent(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
if #available(iOS 13.0, *) {
if viewControllerToPresent.modalPresentationStyle == .automatic {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
}
swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
}
}
用法: 在你的AppDelegate内部应用(_ application: didFinishLaunchingWithOptions)中添加这一行:
UIViewController.swizzlePresent()
使用这种方式,您不需要对任何present调用做任何更改,因为我们正在运行时替换present方法实现。 如果你想知道什么是swizzling,你可以查看这个链接: https://nshipster.com/swift-objc-runtime/
我在启动屏幕后的初始视图中遇到了这个问题。修复我,因为我没有一个segue或逻辑定义是切换演示从自动到全屏如下所示: