【问题标题】:Return to portrait after landscape view横向查看后返回纵向
【发布时间】:2014-05-25 07:09:41
【问题描述】:

我有一个应用程序需要所有视图都处于纵向,而一个视图 (UIWebview) 需要支持纵向和横向。

为了设置默认的纵向,我把它放在我的应用代理中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if(flagOrientationAll == YES){
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}}

并且我已经设法使用这种方法将我的 webview 安排为纵向/横向关于用户方向(也从 stackoverflow 获得)

-(void)viewWillAppear:(BOOL)animated
{

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(@"viewWillDisappear -- Start");
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}

并使用两个 UIView 来呈现纵向和横向:

-(void)viewWillLayoutSubviews {
    BOOL layoutIsPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    if (layoutIsPortrait) {
        self.view = self.pView;
    [self.pView addSubview:pwebView];
}else{
    self.view = self.lView;
    [self.lView addSubview:lwebView];
}}

然后使用以下代码关闭 webview:

- (IBAction)doneButtonTapped:(id)sender
{
   [self.navigationController popViewControllerAnimated:YES];
}

但是,当它以横向状态关闭时,前一个屏幕(调用 web 视图的屏幕)以横向而不是默认(纵向)呈现。 在 webview 关闭后我怎样才能让它仍然以纵向显示?

谢谢

【问题讨论】:

    标签: ios objective-c uiwebview


    【解决方案1】:

    您应该为每个viewController 实现支持的方向:

    就像我写的here:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        //Choose your available orientation, you can also support more tipe using the symbol |
        //e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
        return (UIInterfaceOrientationMaskPortrait);
    }
    

    【讨论】:

      【解决方案2】:

      Swift 3.0:
      (添加到调用横向视图的 ViewController)

      override var shouldAutorotate: Bool {
          return true
      }
      
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return .portrait
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        • 2012-03-05
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多