在iMessage应用程序中检查横向/纵向方向(扩展名) [英] Check Landscape/Portrait orientation in an iMessage app (extension)

查看:93
本文介绍了在iMessage应用程序中检查横向/纵向方向(扩展名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经看到很多方法来检查方向,但奇怪的是,没有工作!!

Have seen a lot of solutions to check the orientation, but strangely, none works!!

下面是代码片段,

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    print("Screen Width = \(screenWidth)")
    print("Screen Height = \(screenHeight)")

    if (screenWidth > screenHeight) {
        print("Landscape")
        alertView.removeFromSuperview()
        messageView.addGestureRecognizer(tapTextView)
    }
    else {
        print("Portrait")
        setupLandscapeAlertView()
    }
}

另一种用于设置视图的方法是,

The other method which is used to setup the view is,

fileprivate func setupLandscapeAlertView() {
    messageView.removeGestureRecognizer(tapTextView)

    alertView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
    alertView.backgroundColor = UIColor.clear
    alertView.isUserInteractionEnabled = false

    let transparentView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.height, height: self.view.frame.width))
    transparentView.backgroundColor = UIColor.gray
    transparentView.backgroundColor = transparentView.backgroundColor!.withAlphaComponent(0.6)
    transparentView.isUserInteractionEnabled = false
    alertView.addSubview(transparentView)

    let blurEffect = UIBlurEffect(style: .regular)
    let blurredEffectView = UIVisualEffectView(effect: blurEffect)
    blurredEffectView.frame = transparentView.bounds
    blurredEffectView.isUserInteractionEnabled = false
    alertView.addSubview(blurredEffectView)

    let imageView = UIImageView(image: UIImage(named: "LandscapeAlert"))
    imageView.frame = CGRect(x: 70, y: 75, width: imageView.frame.width, height: imageView.frame.height)
    imageView.contentMode = UIViewContentMode.scaleAspectFit
    imageView.contentMode = UIViewContentMode.center
    imageView.isUserInteractionEnabled = false
    alertView.addSubview(imageView)

    self.view.addSubview(alertView)
}

另一件事是,此图像不居中。我该怎么做?同样,太多的解决方案,但没有任何工作。

Another thing is that, this image isn't centered. How do I go about that? Again, too many solutions, but nothing does the job.

不确定我做错了什么。 : - /

Not sure if I'm doing anything wrong. :-/

推荐答案

这可能是因为 viewWillTransition 在视图之前执行实际上是翻转。如果你在打印(height width \(view.frame.height)\(view.frame.width)) > viewWillTransition 阻止你会看到。

This may be because viewWillTransition executes before the view actually flips. If you do a simple print("height width \(view.frame.height) \(view.frame.width)") inside the viewWillTransition block you will see.

这篇关于在iMessage应用程序中检查横向/纵向方向(扩展名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆