如何在Swift中旋转UIAlertController

问题描述:

我有一个工作UIAlertController,但我想旋转alert.view左90度。如何在Swift中旋转UIAlertController

我该怎么办?我的代码是在这里如下:

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

我尝试添加:

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

,但它不工作。

谢谢!

有了这个代码:

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

    }) 

你可以做到这一点:

enter image description here

更新答案

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 
     } 

    }) 
+0

谢谢工作,但!有1个问题,当警报显示第一个看起来正常后1秒左右旋转90度。我们如何解决它? – SwiftDeveloper

+0

@SwiftDeveloper - ok兄我知道了,给我一些时间我有一个小工作,我完成了,我开始你的工作 –

+0

谢谢兄弟我们需要解决它,我认为会帮助很多人! – SwiftDeveloper