ios - Swift:如何在纵向模式下正确旋转 UITableViewController 和 UIViewController

标签 ios swift uitableview uiviewcontroller autorotate

在我的 iOS 应用程序中,我希望某些 Controller 仅具有 .Portrait 模式,而且我希望它们在来自允许的 Controller 时以 .Portrait 模式旋转处于 .Landscape 模式。我认为在这些 .Portrait 仅 View Controller 中实现以下方法是正确的方法:

override func viewWillAppear(animated: Bool) {
    self.automaticallyAdjustsScrollViewInsets = true
    super.viewWillAppear(animated)
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")

}

无论如何,我得到的结果是不正确的。例如,以下是 .PortraitUITableViewController 并且当调用 viewWillAppear() 时,而不是这个

enter image description here

我得到以下内容

enter image description here

UIViewController 也会发生这种情况。这就像 View (或表格 View )没有旋转并保持在 .Landscape 模式。我错过了什么?

最佳答案

你玩自动布局吗?当我设置前缘、后缘时,一切都会正常。

另一种解决方案可以手动重新计算textView:

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    textView.frame = CGRect(x: textView.frame.minY, y: textView.frame.minX, width: textView.frame.height, height: textView.frame.width)
}

更新

我测试了之前的解决方案,它不起作用,但我找到了另一种方法。实际上你需要旋转它并再次调用 viewWillAppear - 它对我有用,试试这个:

if orientation.isLandscape{
   let value = UIInterfaceOrientation.Portrait.rawValue
   UIDevice.currentDevice().setValue(value, forKey: "orientation")
   viewWillAppear(false)
}

不 - 它并不适用于所有情况...

我正在这样测试:

  1. 我正在使用应用程序,

  2. 移动设备旋转到横向,在选项卡栏项目中(未检查方向)。之后,我点击我检查方向的选项卡栏项目,然后旋转)

  3. 它旋转得很好,没有你的问题,当我再次这样做时,问题就出现了。但是,当我点击另一个选项卡栏项目并返回时,一切正常 - 我认为我们应该调用一些方法,但我仍然不知道是哪个。

关于ios - Swift:如何在纵向模式下正确旋转 UITableViewController 和 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36333911/

相关文章:

iOS:如何拍摄圆角的 UIView 快照?

ios - 是否可以找到也有Apple设备的用户的联系人?

swift - 关闭 NSWindow 后 WKWebView 不回收

ios - 为什么 UITableView 没有显示在 UIViewController 上?

ios - 如何向 iOS 中的所有 Controller 发送通知

ios - 即使有内容,View Controller 也会变黑

swift - UIView 子类在初始化时固定高度

ios - 为什么 Locale.current.regionCode 为零?

ios - UITableView 在部分添加按钮

iphone - 如何避免在 UITableViewCell 上滑动删除调用 setEditing