如何在Swift中旋转UIAlertController

时间:2023-01-12 20:34:46

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

我有一个工作的UIAlertController,但我想将alert.view旋转90度。

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 !

谢谢 !

2 个解决方案

#1


8  

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

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:

你可以实现这个目标:

如何在Swift中旋转UIAlertController

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


1  

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;

我认为这可能会更常出现在FaceID上。我必须解决这个问题,因为我有一个处于横向模式的应用程序,但是当用户在iPhone上启动应用程序或者当他们必须使用FaceID对应用程序进行身份验证时,它们通常是纵向的。所以我想根据用户拿着手机的方式显示提醒。我的解决方案是扩展UIAlertController类,如下所示;

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

#1


8  

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

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:

你可以实现这个目标:

如何在Swift中旋转UIAlertController

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


1  

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;

我认为这可能会更常出现在FaceID上。我必须解决这个问题,因为我有一个处于横向模式的应用程序,但是当用户在iPhone上启动应用程序或者当他们必须使用FaceID对应用程序进行身份验证时,它们通常是纵向的。所以我想根据用户拿着手机的方式显示提醒。我的解决方案是扩展UIAlertController类,如下所示;

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