教你如何强制显示一个竖屏的不能侧滑返回的SFSafariViewController

强制让控制器竖屏显示

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

// 隐藏系统状态样
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

设置不让SFSafariViewController侧滑返回

Swift:

let viewController = SFSafariViewController(URL: url)  
presentViewController(viewController, animated: true) {  
  
     for view in viewController.view.subviews {  
  
          if let recognisers = view.gestureRecognizers {  
  
               for gestureRecogniser in recognisers where gestureRecogniser is UIScreenEdgePanGestureRecognizer {  
  
                    gestureRecogniser.enabled = false  
               }  
          }  
     }  
 }

OC:

 EFKRSafariViewController *safari = [[EFKRSafariViewController alloc] initWithURL:url];
    safari.closeHandler = completion;
    [self presentViewController:safari animated:YES completion:^{
        // 禁止侧滑返回,因为侧滑返回导致强制竖屏会变回横屏
        for (UIView * view in safari.view.subviews) {
            NSArray<__kindof UIGestureRecognizer *> * array = view.gestureRecognizers;
            if (array.count) {
                for (UIScreenEdgePanGestureRecognizer * sepgr in array) {
                    sepgr.enabled = NO;
                }
            }
        }
    }];

如何

创建继承SFSafariViewController的控制器,在里面添加上面的代码就可以啦!


教你如何强制显示一个竖屏的不能侧滑返回的SFSafariViewController
继承SFSafariViewController.png

总结

有时候,我们在修改系统的控件属性时,总是希望想找到一个属性方法设置后,就可以达到自己想要的 UI或者逻辑,但其实,系统不可以提供那么多自定义的 API,所以,我们只能通过自己去找到想要的东西,然后去修复它,达到目的。类似的思想,比如hook。代码上也是很其妙。

参考扩展


注:本文首发于 iHTCboy's blog,如若转载,请注明来源。

上一篇:最全的iOS真机调试教程(证书生成等)


下一篇:记一次Mac mini折腾过程(鼠键共享,更换SSD)