Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在拆分视图中,如何在Swift中确定应用程序的当前宽度?_Swift_Ipad_Width_Splitview - Fatal编程技术网

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

在拆分视图中,如何在Swift中确定应用程序的当前宽度?,swift,ipad,width,splitview,Swift,Ipad,Width,Splitview,编辑:我有一个项目,上面有一行按钮。通常,按钮在紧凑视图中为5个,在常规视图中为6个。我想在应用程序以1/3分割视图运行时删除一个按钮。如何确定应用程序的宽度 我正在使用此代码确定应用程序在拆分视图(多任务)中的当前宽度: 它可以工作,但不幸的是,applicationFrame在iOS 9中被弃用,因此我尝试用以下内容替换它: override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator c

编辑:我有一个项目,上面有一行按钮。通常,按钮在紧凑视图中为5个,在常规视图中为6个。我想在应用程序以1/3分割视图运行时删除一个按钮。如何确定应用程序的宽度

我正在使用此代码确定应用程序在拆分视图(多任务)中的当前宽度:

它可以工作,但不幸的是,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

您可以只获取父视图的大小

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)
}
让totalButtonWidth:Int
对于self.collectionView.ui视图中的b{
让totalButtonWidth+=b.frame.width+20//其中“20”是按钮之间的间距
} 
如果(currentSize

诸如此类,但我想你可以理解。

@TheValyLeanGroup的答案将在没有干扰视图控制器的情况下起作用。如果存在这种可能性,您应该能够使用
self.view.window.frame.size.width

多亏了ValyreanGroup和David Berry在《我做了一个解决方案》中的重播,该解决方案可以响应界面更改,而无需使用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
        }
    }
//在应用程序打开和其他事件发生时触发
重写函数traitCollectionDidChange(以前的TraitCollection:UITraitCollection?){
设a=self.view.bounds.width
自适应接口(双(a))
}
//当应用程序打开或在拆分视图中打开时未触发,当其他更改发生时触发
重写func ViewWillTransitionSize(大小:CGSize,带TransitionCoordinator协调器:UIViewControllerTransitionCoordinator){
自适应接口(双(大小、宽度))
}
func isHorizontalSizeClassCompact()->Bool{
if(view.traitCollection.horizontalSizeClass==UIUserInterfaceSizeClass.Compact){
返回true//Comapact
}否则{
返回false//Regular
}
}
func自适应接口(当前宽度:双精度){
如果isHorizontalSizeClassCompact(){//Compact
//当sizeclass很紧凑时,执行需要执行的操作

如果currentWidth看起来很粗糙,但是分割currentWidth/2会给你准确的宽度吗?我对分割视图不是很熟悉,但我假设它是从中间分割的。嗨,NSGangster,谢谢你的评论。分割视图可以调整大小,所以我需要知道由于方向改变或分割拖动而发生变化时,当前的宽度是多少由用户决定。嗨,TheValyreanGroup,谢谢你的回答,我得到了混合结果可能是因为我的实现有一些缺陷,我更新了我的答案以更好地描述问题。我添加了一些代码,应该可以帮助你走上正确的轨道。谢谢你的提示David。我在使用代码的时候出错了。我删除了cOmnent.谢谢你,我有一个UISplitViewController,它在某些情况下会崩溃,但是
self.view.bounds.size
会保持不变,我不知道为什么。结果表明,我正在检查的视图控制器保持不变,即使分割视图大小在变化!
    // 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
        }
    }