【问题标题】:Check if app is in Split View swift快速检查应用程序是否处于拆分视图中
【发布时间】:2016-05-31 11:54:56
【问题描述】:

当我的 swift iPhone/iPad 应用程序处于拆分视图或在 iPhone 上运行时,我想让它发生一些事情。基于这个答案(Detect if app is running in Slide Over or Split View mode in iOS 9),我已经想出了如何在 Objective-C 中做到这一点。我没有使用拆分视图控制器(如果拆分视图处于活动状态,则会发生几件次要事情的压力)。是否可以实时检查应用窗口的宽度?

基本上,我只想在窗口宽度低于某个值时在我的应用中发生一些事情。

【问题讨论】:

  • 我从未测试过拆分视图,但请检查 UIScreen.mainScreen().bounds 是否可以做到这一点。
  • 尺码等级不同。
  • 那么,@UlyssesR 有什么想法吗?
  • @nhgirf 我现在能做什么?
  • @JohnRamos 我只能猜测,试试这些: UIScreen.mainScreen().applicationFrame.size.width 或 self.navigationController.navigationBar.frame.size.width

标签: ios swift ipad


【解决方案1】:

据我所知,我已经用 swift 制作了第一个简单的拆分视图检测器(感谢 Ulysses R.)! 所以听从尤利西斯的建议,我使用let width = UIScreen.mainScreen().applicationFrame.size.width 来检测我的应用程序窗口的宽度。为了让计算机一遍又一遍地检查宽度,我每百分之一秒运行一次 NSTimer,然后在宽度高于/低于某个值时进行处理。

为您提供一些测量值(您必须决定使东西出现在上方/下方的宽度): iPhone 6S Plus:414.0
iPhone 6S:375.0
iPhone 5S:320.0
iPad(纵向):768.0
iPad(1/3 拆分视图):320.0
iPad Air 2(1/2 拆分视图):507.0
iPad(横向):1024.0

这是一个代码sn-p:

class ViewController: UIViewController {

var widthtimer = NSTimer()

func checkwidth() {

    var width = UIScreen.mainScreen().applicationFrame.size.width

    if width < 507 { // The code inside this if statement will occur if the width is below 507.0mm (on portrait iPhones and in iPad 1/3 split view only). Use the measurements provided in the Stack Overflow answer above to determine at what width to have this occur.

        // do the thing that happens in split view
        textlabel.hidden = false

    } else if width > 506 {

        // undo the thing that happens in split view when return to full-screen
        textlabel.hidden = true

    }
}


override func viewDidAppear(animated: Bool) {

    widthtimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "checkwidth", userInfo: nil, repeats: true) 
// runs every hundredth of a second to call the checkwidth function, to check the width of the window.

}

override func viewDidDisappear(animated: Bool) {
    widthtimer.invalidate()
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多