3

EDIT: I have a project with a row of buttons on top on it. Usually the buttons are 5 in Compact view and 6 in Regular view. I would like to remove a button when the app runs in 1/3 Split View. How can I determine the width of the app?

I'm using this code to determinate the current width of the app when in Split View (multitasking):

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

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

        print(currentWidth)
    }

It works, but unfortunately applicationFrame is deprecated in iOS 9, so I'm trying to replace it with this:

    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)
    }

The problem is that the first statement gives you the effective width of the app and it's fine, instead the second one, gives you the width of the screen, so you can't use it to learn the real width of the app when it is in Split View.

Would someone know what code would be necessary to replace this deprecated statement?

let currentWidth = UIScreen.mainScreen().applicationFrame.size.width // deprecated
Cue
  • 2,952
  • 3
  • 33
  • 54
  • May seem hacky but would dividing currentWidth / 2 give you the accurate width? Im not super familiar with Split View but I'm assuming it's split down the middle. – NSGangster Jul 28 '16 at 13:36
  • Hi NSGangster, thank you for the comment. The Split View is resizable so I need to know what is the currently width when a change due to a orientation change or split drag by the user happens. – Cue Jul 28 '16 at 16:40

3 Answers3

3

@TheValyreanGroup's answer will work if there are no intervening view controllers mucking with sizes. If that possibility exists you should be able to use self.view.window.frame.size.width

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Thank you for the tip David. I was wrong the point at which to use the code. I removed the comment. – Cue Aug 07 '16 at 12:16
  • Thank you for this, I had a UISplitViewController that would collapse in certain arrangements but `self.view.bounds.size` would stay the same and I couldn't figure out why. It turns out that the view controller I was checking stayed the same size even though the Split View size was changing! – Cody May 30 '19 at 23:02
2

You can just get the size of the parent view.

let currentSize = self.view.bounds.width

That will return the width accurately even in split view.

You can do something like this to determine whether to show or hide a button.

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)
}

Something like that, but i think you can get the idea.

TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • Hi TheValyreanGroup, thank you for your answer, I'm getting mix results maybe because my implementation has some flaws, I updated my answer to describe better the problem. – Cue Jul 28 '16 at 16:52
  • 1
    I've added some code that should help you get on the right track. – TheValyreanGroup Jul 28 '16 at 17:03
2

Thanks to the replay of TheValyreanGroup and David Berry on this page I made a solution that can respond to the interface changes without using the deprecate statement UIScreen.mainScreen().applicationFrame.size.width I post it here with its context to made more clear what is the problem and the (surely improvable) solution. Please post any suggestion and comment you think could improve the code.

    // 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
        }
    }
Community
  • 1
  • 1
Cue
  • 2,952
  • 3
  • 33
  • 54