'UIApplication.shared.statusBarFrame.height'应用扩展程序解决方案导致视图UI问题

时间:2018-03-21 08:04:38

标签: ios swift xcode

我需要在自定义iOS共享扩展程序中获取状态栏和导航栏高度,以便将topHeaderViewshareMessageTableView置于其下方。由于UIApplication.shared.statusBarFrame.height中的共享不适用于应用扩展程序,因此我必须使用here提供的解决方法。

/// Calculate top distance with "navigationBar" and "statusBar" by adding a
/// subview constraint to navigationBar or to topAnchor or superview
/// - Returns: The real distance between topViewController and Bottom navigationBar
func calculateTopDistance() -> CGFloat {

    /// Create view to measure
    let measureView: UIView = UIView()
    measureView.backgroundColor = .clear
    view.addSubview(measureView)

    /// Add needed constraints
    measureView.translatesAutoresizingMaskIntoConstraints = false
    measureView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    measureView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    measureView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    if let nav = navigationController {
        measureView.topAnchor.constraint(equalTo: nav.navigationBar.bottomAnchor).isActive = true
    } else {
        measureView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    }

    /// Force layout
    view.layoutIfNeeded()

    /// Calculate distance
    let distance = view.frame.size.height - measureView.frame.size.height

    /// Remove from superview
    measureView.removeFromSuperview()

    return distance
}

我在主视图控制器的viewDidAppear()中调用了此函数(在viewDidLayoutSubviews()中调用它会使扩展名崩溃):

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // Adjust top header view to accommodate iPhone X's notch.
    topHeaderView.frame.size = CGSize(width: self.view.bounds.width, height: 50 + self.calculateTopDistance())
    shareMessageTableView.frame.origin = CGPoint(x: 0, y: topHeaderView.frame.height)
    if #available(iOS 11.0, *) {
        shareMessageTableView.frame.size = CGSize(width: self.view.bounds.width, height: self.view.bounds.height - topHeaderView.frame.height - self.view.safeAreaInsets.bottom)
    } else {
        shareMessageTableView.frame.size = CGSize(width: self.view.bounds.width, height: self.view.bounds.height - topHeaderView.frame.height)
    }
}

然后我有第二个视图控制器(照片预览屏幕),我使用.instantiateViewController.pushViewController以编程方式推送,而不是IB中的segue。在这个控制器中,我设置了一个键盘通知,调用这些函数来调整视图 - 这样我就可以在显示整个图像的同时适应键盘:

/// Delegate function when keyboard will show.
///
/// - Parameter notification: Notification
@objc func keyboardWillShow(_ notification: NSNotification) {
    keyboardIsVisible = true
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        self.keyboardSize = keyboardSize
        adjustViews()
    }
}

func adjustViews() {
    if self.keyboardIsVisible && self.keyboardSize != nil {
        self.view.frame.size.height = UIScreen.main.bounds.height - self.keyboardSize!.height
    } else {
        self.view.frame.size.height = UIScreen.main.bounds.height
    }
}

所以这是按预期工作,直到我开始更改方向。一旦方向改变(无论从哪里到哪里),视图将不再调整以显示整个图像即使adjustViews()仍被调用。相反,键盘会出现并覆盖部分图像。当我在顶部实现calculateTopDistance()函数时,这个问题就开始发生了,所以我需要有关如何修复此函数的帮助和建议。我不知道有任何其他的解决方法,所以这对我来说是最好的。

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决问题的方法。我只是将scrollview(包含imageview)的底部约束调整到键盘的高度,而不是调整整个视图。完成!