iosswiftswift3uialertviewuialertcontroller

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 !


Solution

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