2016-08-23 62 views
7

我有一個工作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)) 

,但它不工作。

謝謝!

回答

8

有了這個代碼:

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

相關問題