【问题标题】:Hide bottom table view when orientation changed to landscape mode in Swift ios在Swift ios中方向更改为横向模式时隐藏底部表格视图
【发布时间】:2015-09-08 07:54:26
【问题描述】:

我有一个 UIView 和一个 UITableView 在纵向模式下共享相同高度的屏幕。

现在,当方向更改为横向模式时,如何隐藏表格视图并用 UIView 填充整个屏幕。并以纵向模式恢复 UITableView 和 UIView。

提前致谢。

【问题讨论】:

    标签: ios swift orientation nslayoutconstraint


    【解决方案1】:

    我建议你在 ViewController 中实现 viewWillTransitionToSize

    var landscapeViewFrame = CGRect()
    var landscapeTableViewFrame = CGRect()
    var portraitViewFrame = CGRect()
    var portraitTableViewFrame = CGRect()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        landscapeViewFrame = CGRectMake(0, 0, view.frame.width / 2, view.frame.height)
        landscapeTableViewFrame = CGRectMake(view.frame.width, 0, view.frame.width / 2, self.view.frame.height);
        portraitViewFrame = view.bounds
        portraitTableViewFrame = CGRectMake(0,0,0,0);
    }
    
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        let isPortrait = (size.height > size.width);
        yourView.frame = isPortrait ? portraitViewFrame : landscapeViewFrame
        yourTableView.frame = isPortrait ? portraitTableViewFrame : landscapeTableViewFrame
        yourTableView.hidden = isPortrait
    }
    

    viewWillTransitionToSize 通知您的 viewController 视图框架将发生变化。 UIViewControllerTransitionCoordinator 包含有关动画的信息,例如持续时间/如果有动画。

    更新现在迅速。 基本思路:先计算指定帧。在设备旋转更改时,会调用 viewWillTransitionToSize 并可以设置视图的框架。如前所述,您可能需要动画来使一切看起来流畅

    【讨论】:

    • 我想在横向模式下隐藏 UITableView 并像以前一样在纵向模式下再次显示表格视图。如果您能为我提供快速的解决方案,那将会更有帮助。不管怎么说,还是要谢谢你。 @MarkHim
    • 用更详细的 swift 代码替换了代码。希望对你有帮助
    【解决方案2】:

    1.首先在yourProject中设置设备旋转>General>设备旋转

    2 写在你的视图中会出现

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)`  name:UIDeviceOrientationDidChangeNotification  object:nil];
    
    1. 将这些方法写入您想要的框架或要隐藏或取消隐藏的视图

      - (void)orientationChanged:(NSNotification *)notification
      {
          [self handleOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
      }
      
      
      - (void) handleOrientation:(UIInterfaceOrientation) orientation {
      
           if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
           {
               //handle the portrait view
           }
           else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
           {
                //handle the landscape view
           }
      }
      

    【讨论】:

    • 我已经设置了 NSNotification center 。我想要的只是解决您的解决方案中的 [// 处理纵向视图] 和 [// 处理横向视图] 部分。谢谢@jagveer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多