【问题标题】:Check Landscape/Portrait orientation in an iMessage app (extension)检查 iMessage 应用程序中的横向/纵向方向(扩展)
【发布时间】:2017-11-21 18:42:50
【问题描述】:

已经看到很多检查方向的解决方案,但奇怪的是,没有一个有效!

下面是代码sn-p,

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

另一件事是,这张图片没有居中。我该怎么做?同样,解决方案太多,但没有任何作用。

不确定我是否做错了什么。 :-/

【问题讨论】:

    标签: ios swift swift3 uiimageview orientation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2014-09-10
      • 1970-01-01
      • 2014-09-16
      • 2020-11-25
      • 2015-06-14
      • 2016-03-05
      相关资源
      最近更新 更多