2017-06-19 64 views
0

已经看到很多解决方案来检查方向,但奇怪的是,没有任何工作!在iMessage应用程序中检查横向/纵向(扩展名)

下面是代码片段,

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

的另一件事是,该图像不居中。我该如何解决这个问题?再次,太多的解决方案,但没有任何工作。

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

回答

0

这可能是因为viewWillTransition在视图实际翻转之前执行。如果在viewWillTransition块内部执行简单的print("height width \(view.frame.height) \(view.frame.width)"),您将看到。

相关问题