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

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

问题描述

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

Below is the code snippet,

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. :-/

解决方案

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天全站免登陆