检查 iMessage 应用程序中的横向/纵向方向(扩展)

Check Landscape/Portrait orientation in an iMessage app (extension)(检查 iMessage 应用程序中的横向/纵向方向(扩展))
本文介绍了检查 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 应用程序中的横向/纵向方向(扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)