当webview显示ImagePicker时,不调用presentViewController

时间:2018-08-09 19:06:43

标签: ios webview uiimagepickercontroller

我已经看到由于presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{调用而显示图像选择器时应该调用函数UIWebView的几个问题。

这是我的代码。

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{

   if([viewControllerToPresent.class isEqual:[UIImagePickerController class]]){
       UIImagePickerController *controller = (UIImagePickerController*)viewControllerToPresent;

       controller.navigationBar.tintColor = UIColor.whiteColor;
       [controller.navigationBar setTitleTextAttributes:
        @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
       [controller.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
       [controller.navigationBar setBarTintColor:[UIColor blackColor]];
   }

   [super presentViewController:viewControllerToPresent animated:flag completion:completion];
}

2 个答案:

答案 0 :(得分:0)

当您在网络视图中使用ViewController标签并单击结果按钮时,保存您的网络视图的<input type="file">将调用该方法。

此方法已经在UIViewController中实现,但是如果您想覆盖它,可以在您的UIViewController子类上进行。迅速:

override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        super.present(viewControllerToPresent, animated: flag, completion: completion)
    }

在Objective-C中:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    [super presentViewController:viewControllerToPresent animated:flag completion:completion];
}

值得注意的是,当我在iOS11模拟器上测试该控制器时,该控制器不是UIImagePickerController,而实际上是UIDocumentMenuViewController,因此上面的自定义代码无法正常工作。

此外,请注意,您不应该再使用UIWebView。您应该改用WKWebView

答案 1 :(得分:0)

该事件将在您的ROOT视图上调用,该视图可能是WebViewController,也可能不是WebViewController。使用布局检查器找到您的根视图,或以编程方式从应用程序委托中检索根视图。