Ios 在iMessage应用程序(扩展)中检查横向/纵向方向
我们已经看到了很多检查方向的解决方案,但奇怪的是,没有一个是有效的 下面是代码片段Ios 在iMessage应用程序(扩展)中检查横向/纵向方向,ios,swift,swift3,uiimageview,orientation,Ios,Swift,Swift3,Uiimageview,Orientation,我们已经看到了很多检查方向的解决方案,但奇怪的是,没有一个是有效的 下面是代码片段 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { let screenSize = UIScreen.main.bounds let screenWidth = screenSize.width let screenHeig
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)
}
另一件事是,这个图像没有居中。我该怎么办?同样,有太多的解决方案,但没有任何解决方案
不确定我是否做错了什么-/ 这可能是因为
viewWillTransition
在视图实际翻转之前执行。如果执行简单的打印(“height-width\(view.frame.height)\(view.frame.width)”)
在viewWillTransition
块内,您将看到