如何在拆分视图中确定Swift中应用程序的当前宽度?

时间:2016-07-28 13:27:37

标签: swift ipad width splitview

编辑:我的项目顶部有一排按钮。通常,紧凑视图中的按钮为5,常规视图中的按钮为6。当应用程序在1/3 Split View中运行时,我想删除一个按钮。如何确定应用的宽度?

我在分割视图(多任务处理)中使用此代码来确定应用的当前宽度:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        // works but it's deprecated:
        let currentWidth = UIScreen.mainScreen().applicationFrame.size.width

        print(currentWidth)
    }

虽然有效,但遗憾的是appFrame在iOS 9中已弃用,所以我试图将其替换为:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        // gives you the width of the screen not the width of the app:
        let currentWidth = UIScreen.mainScreen().bounds.size.width

        print(currentWidth)
    }

问题是,第一个语句为您提供了应用程序的有效宽度,它很好,而第二个语句为您提供了屏幕的宽度,因此您无法使用它来学习应用程序的实际宽度当它在Split View中时。

有人知道替换这个已弃用的语句需要哪些代码吗?

let currentWidth = UIScreen.mainScreen().applicationFrame.size.width // deprecated

3 个答案:

答案 0 :(得分:3)

@ TheValyreanGroup的答案将起作用,如果没有干预视图控制器与大小混淆。如果存在这种可能性,您应该能够使用self.view.window.frame.size.width

答案 1 :(得分:2)

您只需获取父视图的大小即可。

let currentSize = self.view.bounds.width

即使在拆分视图中也能准确返回宽度。

您可以执行以下操作来确定是否显示或隐藏按钮。

let totalButtonWidth: Int
for b in self.collectionView.UIViews{
    let totalButtonWidth += b.frame.width + 20 //Where '20' is the gap between your buttons
} 
if (currentSize < totalButtonWidth){
    self.collectionView.subviews[self.collectionView.subviews.count].removeFromSuperview()
}else{
    self.collectionView.addSubview(buttonViewToAdd)
}

类似的东西,但我认为你可以得到这个想法。

答案 2 :(得分:2)

感谢TheValyreanGroup和David Berry在this page上的重播,我提出了一个解决方案,可以在不使用弃用语句UIScreen.mainScreen().applicationFrame.size.width的情况下响应接口更改。我在此处发布它的上下文以使其更加清晰什么是问题和(肯定是可以改进的)解决方案。请发布您认为可以改进代码的任何建议和评论。

    // trigged when app opens and when other events occur
    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        let a = self.view.bounds.width
        adaptInterface(Double(a))
    }

    // not trigged when app opens or opens in Split View, trigged when other changes occours
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        adaptInterface(Double(size.width))
    }

    func isHorizontalSizeClassCompact () -> Bool {
        if (view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact) {
            return true // Comapact
        } else {
            return false // Regular
        }
    }

    func adaptInterface(currentWidth: Double) {
        if isHorizontalSizeClassCompact() { // Compact
            // do what you need to do when sizeclass is Compact
            if currentWidth <= 375 {
                // do what you need when the width is the one of iPhone 6 in portrait or the one of the Split View in 1/3 of the screen
            } else {
                // do what you need when the width is bigger than the one of iPhone 6 in portrait or the one of the Split View in 1/3 of the screen
            }
        } else { // Regular
        // do what you need to do when sizeclass is Regular
        }
    }