UIKit - 如何确定分屏视图中应用程序窗口的宽度?

问题描述 投票:0回答:3

我有一个项目,UI 顶部有一排按钮。紧凑视图中显示五个按钮,常规视图中显示六个按钮。我想在应用程序以 1/3 分割视图运行时删除一个按钮。

如何确定应用程序窗口的宽度?

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

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        
        // works but it's deprecated:
        let currentWidth = UIScreen.mainScreen().applicationFrame.size.width
        
        print(currentWidth)
    }

这可行,但

UIScreen.mainScreen().applicationFrame
在 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)
    }

什么代码可以替换已弃用的语句?

let currentWidth = UIScreen.mainScreen().applicationFrame.size.width // deprecated
uikit width ios9 splitview
3个回答
3
投票

@TheValyreanGroup 的答案将在没有干预视图控制器对尺寸进行修改的情况下起作用。 如果存在这种可能性,您应该能够使用

self.view.window.frame.size.width


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
投票

感谢 TheValyreanGroup 和 David Berry 在 this page 上的重播我制定了一个解决方案,可以在不使用 deprecate 语句的情况下响应界面更改

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
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.