【问题标题】:How to rotate UIAlertController in Swift如何在 Swift 中旋转 UIAlertController
【发布时间】:2016-08-23 10:30:25
【问题描述】:

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

我该怎么做?我的代码如下:

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

但它不起作用。

谢谢!

【问题讨论】:

    标签: ios swift swift3 uialertview uialertcontroller


    【解决方案1】:

    使用此代码:

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

    你可以做到这一点:

    更新答案

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

    【讨论】:

    • 谢谢你的工作,但是!向左转 90 度 1 秒后首次显示正常的警报时出现 1 个问题。我们如何解决它?
    • @SwiftDeveloper - 好的,兄弟,我知道了,给点时间我有一个小工作,我完成并开始你的工作
    • 谢谢兄弟,我们需要解决它,我认为这会帮助很多人!
    • @SwiftDeveloper - 根据需要更改时间间隔,
    • 我将间隔更改为 0 不要像第一个代码那样更改任何东西,一旦正常转动 90 度 :( 不要先打开 90 度
    【解决方案2】:

    我认为 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
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果您的应用程序支持landscapeRight、landscapeLeft 和默认(作为纵向),您可以试试这个

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

      这将根据您的屏幕旋转旋转您的警报视图,而不会产生任何 UI 动画问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-14
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 2015-06-16
        相关资源
        最近更新 更多