Why does my iPad layout not readjust upon rotation?
Asked Answered
A

1

6

I have a Split View controller for iPad that should support both orientations (portrait and landscape).

The problem is that when I change the device's orientation the details view doesn't readjust to use the whole space and a grey square appears.

enter image description here

I don't think is a problem with the constraints because this only happens when I rotate:

  • When I launch the app in portrait mode the layout loads good.
  • When I launch the app in landscape mode the layout loads good.

enter image description here

In the SplitViewController (and also in the DetailsController) I tried this code:

In the viewDidLoad:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)

and then:

override func shouldAutorotate() -> Bool {
    print("should rotate --> true")
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    print("we support all orientations")
    return UIInterfaceOrientationMask.All
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    print("preferred orientation for presentation is landscape")
    return UIInterfaceOrientation.LandscapeLeft
}

func rotated(){
    print("rotatiooooonn ------------")
    refreshIpad()
    detailSplitViewController.refreshUI()
}

func refreshIpad (){
    dispatch_async(dispatch_get_main_queue(),{
        self.view.setNeedsLayout()
    })
}

In the logs I get this (so I can see whenever the change in orientation happens)

should rotate --> true
we support all orientations
rotatiooooonn ------------

Below is the info file:

enter image description here

EDIT

Storyboard: enter image description here

Constrains in Details View:

enter image description here

Angadreme answered 20/7, 2016 at 9:47 Comment(5)
Could you post an image of your storyboard with constraints or wherever you show your constraints.Calondra
@Calondra just added the pics of storyboard and constrains. I hope we can solve it :)Angadreme
In the console does it complain about any constraints breaking? Do you load the splitviewcontroller contents into another view? It looks spiltviewcontroller has not adjusted correctly in the landscape image and it looks like only the detail part of the splitviewcontroller is not correct in the portrait image. As a test change the constraints of the container for the detail view to have a constant of 0 for top, bottom, leading and trailing and see what happens.Calondra
Hello @Calondra , it does not complain about constraints, I did changed them as you said and still the same problem. We do not load the split view inside anything else, although we do load another view (from another storyboard) inside the container of the details. What should I do? I've been stuck for days :(Angadreme
Are you able to put a sample of your project on github or something? All I can suggest is create another project and bit by bit rebuild your project, from what I can see it should be okay, start with only a storyboard, no custom viewcontrollers and see if that works. Otherwise if you can post a sample project I can try help you debug.Calondra
T
0

In refreshIpad() you need to explicitly call setNeedsLayout() for all your subviews as it doesn't happens recursively,

func refreshIpad() {
    dispatch_async(dispatch_get_main_queue(),{
        self.view.setNeedsLayout()
        for subview in self.view.subviews {
            subview.setNeedsLayout()
        }
    })
}

Recursive solution (Swift 3.0)

extension UIView {
    func setNeedsLayoutForSubviews() {
        self.subviews.forEach({
            $0.setNeedsLayout()
            $0.setNeedsLayoutForSubviews()
        })
    }
}

func refreshIpad() {
    DispatchQueue.main.async {
        self.view.setNeedsLayout()
        self.view.setNeedsLayoutForSubviews()
    }
}
Tombolo answered 27/5, 2017 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.