在iMessage应用程序中检查风景/纵向(扩展名)

已经看到很多解决scheme来检查方向,但奇怪的是,没有任何工作!

以下是代码片段,

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

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

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

另一件事是,这个形象不居中。 我如何去做呢? 再次,太多的解决scheme,但没有任何工作。

不知道我是否做错了什么。 : – /

这可能是因为viewWillTransition在视图实际翻转之前执行。 如果你在viewWillTransition块中做一个简单的print("height width \(view.frame.height) \(view.frame.width)") ,你会看到。