首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Swift中旋转UIAlertController

如何在Swift中旋转UIAlertController
EN

Stack Overflow用户
提问于 2016-08-23 18:30:25
回答 2查看 1.9K关注 0票数 7

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

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

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

我试着补充说:

代码语言:javascript
复制
 alert.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

但它不起作用。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2018-06-01 07:02:00

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-01-24 19:55:17

如果您的应用程序支持landscapeRight、landscapeLeft和default (如纵向),您可以尝试这样做

代码语言:javascript
复制
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动画问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39098700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档