pas*_*ros 280 viewcontroller modalviewcontroller ios ios13
在iOS 13 Beta 1中,出现模态视图控制器时有新行为。现在默认情况下它不是全屏显示,当我尝试向下滑动时,该应用程序会自动关闭View Controller。
如何防止这种行为并回到旧的好全屏模式vc?
谢谢
pas*_*ros 402
如WWDC 2019期间平台联盟中所述,使用iOS 13,Apple引入了新的默认卡演示文稿。为了强制全屏,您必须使用以下命令明确指定它:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Ale*_*dro 116
我添加了对某人可能有用的信息。如果您有任何故事板segue,那么要回到旧样式,需要将kind属性设置为Present Modally并将Presentation属性设置为Full Screen。
dav*_*tes 67
在启动屏幕之后的初始视图中,我遇到了这个问题。因为没有定义顺序或逻辑,所以对我的解决方法是将演示文稿从自动切换到全屏,如下所示:
Abe*_*eyh 52
有多种方法可以做到这一点,我认为每个项目都可以适合一个项目,但又不适合另一个项目,所以我认为我会保留在这里,也许其他人会遇到不同的情况。
如果您有一个BaseViewController
,则可以覆盖该present(_ viewControllerToPresent: 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)
}
// ....
}
Run Code Online (Sandbox Code Playgroud)
使用这种方式,您无需对任何present
调用进行任何更改,因为我们只是覆盖了该present
方法。
extension UIViewController {
func presentInFullScreen(_ viewController: UIViewController,
animated: Bool,
completion: (() -> Void)? = nil) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: animated, completion: completion)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
presentInFullScreen(viewController, animated: true)
Run Code Online (Sandbox Code Playgroud)
let viewController = UIViewController()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
在您的AppDelegate
内部application(_ application: didFinishLaunchingWithOptions)
添加以下行:
UIViewController.swizzlePresent()
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您不需要在任何当前调用上进行任何更改,因为我们正在运行时替换当前方法的实现。
如果您想知道什么在泛滥,可以查看以下链接:https :
//nshipster.com/swift-objc-runtime/
Pra*_*dha 43
一个班轮:
modalPresentationStyle
需要在正在呈现的导航控制器 上设置。
iOS 13 及以下 iOS 版本 fullScreen
overCurrentContext
和navigationController
测试代码
let controller = UIViewController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(navigationController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
modalPresentationStyle需要在navigationController 中设置。
9to*_*ios 25
对于Objective-C用户
只需使用此代码
[vc setModalPresentationStyle: UIModalPresentationFullScreen];
Run Code Online (Sandbox Code Playgroud)
或者,如果要在iOS 13.0中添加它,请使用
if (@available(iOS 13.0, *)) {
[vc setModalPresentationStyle: UIModalPresentationFullScreen];
} else {
// Fallback on earlier versions
}
Run Code Online (Sandbox Code Playgroud)
mum*_*umu 25
适用于 iOS 13 和 Swift 5.x 的最新版本
let vc = ViewController(nibName: "ViewController", bundle: nil)
Run Code Online (Sandbox Code Playgroud)
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
kuz*_*zdu 20
提示:如果您对存在于ViewController
内嵌的的调用present ,则NavigationController
必须将设置为NavigationController
,.fullScreen
而不是VC。
您可以像@davidbates一样执行此操作,也可以通过编程方式(例如@pascalbros)来执行。
一个示例场景:
//BaseNavigationController: UINavigationController {}
let baseNavigationController = storyboard!.instantiateViewController(withIdentifier: "BaseNavigationController")
var navigationController = UINavigationController(rootViewController: baseNavigationController)
navigationController.modalPresentationStyle = .fullScreen
navigationController.topViewController as? LoginViewController
self.present(navigationViewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Gur*_*i S 20
这对我有用
let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboardID_cameraview1") as! CameraViewController
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)`
Run Code Online (Sandbox Code Playgroud)
Ran*_*jaz 18
快速解决。上面已经有很好的答案。我还添加了我的快速 2 点输入,它显示在屏幕截图中。
如果您不使用Navigation Controller
,Right Menu Inspector
则将 Presentation 设置为Full Screen
如果您正在使用,Navigation Controller
则默认情况下它将全屏显示,您无需执行任何操作。
Max*_*rov 16
我在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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后把这个
UIViewController.preventPageSheetPresentation
Run Code Online (Sandbox Code Playgroud)
某处
例如在AppDelegate中
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UIViewController.preventPageSheetPresentation
// ...
return true
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的解决方案,无需编码一行。
此更改使iPad应用程序的行为符合预期,否则新屏幕将在弹出窗口的中央显示。
这是我在 ObjectiveC 中使用类别进行修复的版本。使用这种方法,您将拥有默认的 UIModalPresentationStyleFullScreen 行为,直到另一个明确设置。
#import "UIViewController+Presentation.h"
#import "objc/runtime.h"
@implementation UIViewController (Presentation)
- (void)setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
[self setPrivateModalPresentationStyle:modalPresentationStyle];
}
-(UIModalPresentationStyle)modalPresentationStyle {
UIModalPresentationStyle style = [self privateModalPresentationStyle];
if (style == NSNotFound) {
return UIModalPresentationFullScreen;
}
return style;
}
- (void)setPrivateModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
NSNumber *styleNumber = [NSNumber numberWithInteger:modalPresentationStyle];
objc_setAssociatedObject(self, @selector(privateModalPresentationStyle), styleNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIModalPresentationStyle)privateModalPresentationStyle {
NSNumber *styleNumber = objc_getAssociatedObject(self, @selector(privateModalPresentationStyle));
if (styleNumber == nil) {
return NSNotFound;
}
return styleNumber.integerValue;
}
@end
Run Code Online (Sandbox Code Playgroud)
设置navigationController.modalPresentationStyle
为.fullScreen
已经在这里重复了一千多次,但让我向您展示另一个阻止程序,它导致即使所有属性都设置正确,UIViewController
/UINavigationController
也没有显示。fullscreen
就我而言,罪魁祸首隐藏在这一行中
navigationController?.presentationController?.delegate = self
Run Code Online (Sandbox Code Playgroud)
显然,在设置时,UIAdaptivePresentationControllerDelegate
您需要在可选委托方法中指定呈现样式
public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
presentationStyle
}
Run Code Online (Sandbox Code Playgroud)
所有其他答案都足够了,但是对于像我们这样的大型项目来说,无论是在代码还是在情节提要中进行导航,这都是一项艰巨的任务。
对于那些正在积极使用Storyboard的人。这是我的建议:使用Regex。
以下格式不适用于全屏页面:
<segue destination="Bof-iQ-svK" kind="presentation" identifier="importSystem" modalPresentationStyle="fullScreen" id="bfy-FP-mlc"/>
Run Code Online (Sandbox Code Playgroud)
以下格式适合全屏页面:
<segue destination="7DQ-Kj-yFD" kind="presentation" identifier="defaultLandingToSystemInfo" modalPresentationStyle="fullScreen" id="Mjn-t2-yxe"/>
Run Code Online (Sandbox Code Playgroud)
以下与VS CODE兼容的正则表达式会将所有旧样式页面转换为新样式页面。如果您使用其他正则表达式引擎/文本编辑器,则可能需要转义特殊字符。
搜索正则表达式
<segue destination="(.*)"\s* kind="show" identifier="(.*)" id="(.*)"/>
Run Code Online (Sandbox Code Playgroud)
替换正则表达式
<segue destination="$1" kind="presentation" identifier="$2" modalPresentationStyle="fullScreen" id="$3"/>
Run Code Online (Sandbox Code Playgroud)
小智 5
这是Objective-C的解决方案
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
正如 WWDC 2019 期间的平台联盟声明所述,Apple 在 iOS 13 中引入了新的默认卡片呈现方式。为了强制全屏,你必须明确指定它:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.navigationViewController.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70208 次 |
最近记录: |