Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate UIAlertController in Swift

I have a working UIAlertController, but I want to rotate the alert.view by 90 degrees left.

How can I do it? My code is here below:

let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Default){(action)->() in })
presentViewController(alert, animated: true) {}

I tried to add:

 alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

but it doesn't work.

Thank you !

like image 616
SwiftDeveloper Avatar asked Aug 23 '16 10:08

SwiftDeveloper


3 Answers

With this code:

let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Default){(action)->() in })

self.presentViewController(alert, animated: true, completion: {() -> Void in
      alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

})

Swift3

let alert = UIAlertController(title: "", message: "Message Sample", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Okay", style: .default){(action)->() in })

    self.present(alert, animated: true, completion: {() -> Void in
        alert.view.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2) )

    })

You can achieve this:

enter image description here

Updated answer

 self.presentViewController(alert, animated: true, completion: {() -> Void in
         // alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

        UIView.animateWithDuration(1.0, delay: 0, options: .CurveLinear, animations: { () -> Void in
            alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
        }) { (finished) -> Void in
          // do something if you need
        }

    })
like image 95
Anbu.Karthik Avatar answered Nov 20 '22 02:11

Anbu.Karthik


I think this may come up more often with FaceID. I had to solve this problem because I have an app that is in landscape mode, but when the user starts the app on an iPhone or when they have to authenticate to the app with FaceID they are typically in portrait. So I wanted to display alerts based on how the user was holding the phone. My solution was to extend the UIAlertController class as follows;

extension UIAlertController {
    open override func viewWillAppear(_ animated: Bool) {
        view.isHidden = true
    }
    override open func viewDidAppear(_ animated: Bool) {
        let appDirection = UIApplication.shared.statusBarOrientation
        let deviceDirection = UIDevice.current.orientation
        var direction:CGFloat = 0
        if deviceDirection == .portrait {
            if appDirection == .landscapeLeft {
                direction = 1.0
            } else if appDirection == .landscapeRight {
                direction = -1.0
            }
        } else if deviceDirection == .portraitUpsideDown {
            if deviceDirection == .landscapeLeft {
                direction = -1.0
            } else if appDirection == .landscapeRight {
                direction = 1.0
            }
        }
        if direction != 0 {
           view.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2) * direction);
        }
        view.isHidden = false
    }
    open override func viewWillDisappear(_ animated: Bool) {
        view.isHidden = true
    }
}
like image 1
Nico Nierenberg Avatar answered Nov 20 '22 01:11

Nico Nierenberg


If your application supports landscapeRight, landscapeLeft and default (as portrait), you can try this

self.present(alertController, animated: false, completion: {
            switch UIDevice.current.orientation {
            case .landscapeRight:
                self.alertController.view.transform=CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 2))
            case .landscapeLeft:
                self.alertController.view.transform=CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
            default:
                self.alertController.view.transform=CGAffineTransform.identity
            }
        })

This will rotate your alert view as per your screen rotation without making any UI animation issue.

like image 1
Nupur Parekh Avatar answered Nov 20 '22 01:11

Nupur Parekh