JMD
JMD

Reputation: 1590

How to stop rotation using viewWillTransitionToSize

With iOS 8 came two new methods for handling screen rotations.

ViewWillTransitionToSize and willTransitionToTraitCollection.

My question is, how do you halt rotations in these methods? is there some boolean we flag? are we forced to manually set a specific orientation?

Upvotes: 3

Views: 3440

Answers (2)

DEG
DEG

Reputation: 1

JMD, I have the same question/problem as you, that is, disabling autorotation for a specific screen, but enabling it for all others. As you indicated, supportedInterfaceOrientations( ) in iOS 8 doesn't seem to do anything to solve the problem.

I think that the code below using viewWillTransitionToSize would work IF, like you, I knew how to change the Boolean property for shouldAutorotate; when I attempted to do just that using the code

self.shouldAutorotate( ).boolValue = true

I got the error message "cannot assign to the result of this expression". When I comment that code, I get the results below, so I know that viewWillTransitionToSize is working:

Rotating the Device in the Simulator from Portrait -> LandscapeLeft gives the console message:

will transition size change to (667.0,375.0)

shouldAutorotate Bool = true

height < width: transitioning size change to (667.0,375.0)

Then rotating the Device in the Simulator from LandscapeLeft -> Portrait gives

will transition size change to (375.0,667.0)

shouldAutorotate Bool = true

height > width: transitioning size change to (375.0,667.0)

I know that this doesn't completely answer your question, but maybe someone else can fill in the blanks.

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


    println("will transition size change to \(size)")
    var autorotateBoolValue = self.shouldAutorotate()
    println("shouldAutorotate Bool = \(autorotateBoolValue)")

    if (size.height > size.width) {

        coordinator.animateAlongsideTransition({context in

            println("height > width: transitioning size change to \(size)")}, completion: {context in
            self.shouldAutorotate().boolValue = true

        })

    }

    else {

        coordinator.animateAlongsideTransition({context in

            println("height < width: transitioning size change to \(size)")}, completion: {context in
            self.shouldAutorotate().boolValue = false

        })

    }

}

Upvotes: 0

soulshined
soulshined

Reputation: 10612

viewWillTransitionToSize:withTransitionCoordinator: is for notifying the container that the size of its view is about to change so you could perform tasks related to size change. Orientation specific results should be called in your VC with the following:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

EDIT this is the correct method to call and has been available since >= iOS 6.0 to lock screen orientation programmatically.

I would answer this and know the answer, but since it's a duplicate question, i feel it's due process to allow the original answer the up vote and credit:

See here : How do we dictate app orientation in iOS 8?

Upvotes: 2

Related Questions