How to rotate UIAlertController in Swift

故事扮演 提交于 2019-12-09 17:04:50

问题


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 !


回答1:


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:

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
        }

    })



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/39098700/how-to-rotate-uialertcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!