为什么顶部布局指南会在我的iMessage扩展中移动 [英] Why is the top layout guide moving in my iMessage extension

查看:146
本文介绍了为什么顶部布局指南会在我的iMessage扩展中移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iMessage扩展程序,我在顶部布局指南中遇到了一些问题。我有一个 MSMessagesAppViewController 来处理表示样式之间的变化。在我的扩展中,我有一个按钮。单击它时,我转换为展开的演示文稿样式,然后以模态方式呈现视图控制器。问题在于:第二个VC中的UI隐藏在顶部导航栏后面。我认为这很奇怪,因为我将约束设置为顶部布局指南。所以我挖掘了我的代码并开始调试顶层布局指南。我注意到在转换到扩展的演示文稿样式后, topLayoutGuide.length = 86.应该是这样的。但是当我以模态方式呈现第二个视图控制器时,顶部布局指南被重置为0.为什么它不应该是86呢?这是我的代码:

I have an iMessage extension and I'm having some issues with the top layout guide. I have an MSMessagesAppViewController that handles changes between presentation styles. In my extension I have a button. When it is clicked I transition to expanded presentation style and then present a view controller modally. Here's the problem: my UI in the second VC is getting hidden behind the top navigation bar. I thought this was strange as I set my constraints to the top layout guide. So I dug through my code and started debugging the top layout guide. I noticed that after I transition to expanded presentation style, topLayoutGuide.length = 86. That's how it should be. But when I present the second view controller modally, the top layout guide is reset to 0. Why isn't it 86 as it should be? Here is my code:

在我的主viewController中:

In my main viewController:

@IBAction func addStickerButtonPressed(_ sender: AnyObject) {
    shouldPerformCreateSegue = true
    theSender = sender
    requestPresentationStyle(.expanded)

}    
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    if presentationStyle == .expanded {
        if shouldPerformCreateSegue == true {
            shouldPerformCreateSegue = false
            performSegue(withIdentifier: "CreateStickerSegue", sender: theSender)//here is where I present the new viewController
        } else {
            searchBar.becomeFirstResponder()
            searchBar.placeholder = nil
            searchBar.showsCancelButton = true
            searchBar.tintColor = UIColor.white
        }
    } else {
        searchBar.showsCancelButton = false
    }
    print(topLayoutGuide.length) //This prints out 86
}

在另一个模态显示的视图控制器中:

In the other modally presented view controller:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.view.addConstraint(navBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor))
    print(topLayoutGuide.length) //This prints out 0
}


推荐答案

作为一种解决方法,我使用 UIPresentationController ,这会改变模态视图控制器 topLayoutGuide.length points:

As a workaround I use UIPresentationController, which shifts the modal view controller by topLayoutGuide.length points:

class MyViewController: MSMessagesAppViewController {

    private func presentModalViewController() {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .savedPhotosAlbum

        imagePicker.modalPresentationStyle = .custom
        imagePicker.transitioningDelegate = self

        present(imagePicker, animated: true, completion: nil)
    }
}
// MARK: - UIViewControllerTransitioningDelegate
extension MyViewController: UIViewControllerTransitioningDelegate {

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let vc = PresentationController(presentedViewController: presented, presenting: presenting)
        // I really don't want to hardcode the value of topLayoutGuideLength here, but when the extension is in compact mode, topLayoutGuide.length returns 172.0.
        vc.topLayoutGuideLength = topLayoutGuide.length > 100 ? 86.0 : topLayoutGuide.length
        return vc
    }
}


class PresentationController: UIPresentationController {

    var topLayoutGuideLength: CGFloat = 0.0

    override var frameOfPresentedViewInContainerView: CGRect {
        guard let containerView = containerView else {
            return super.frameOfPresentedViewInContainerView
        }
        return CGRect(x: 0, y: topLayoutGuideLength, width: containerView.bounds.width, height: containerView.bounds.height - topLayoutGuideLength)
    }
}

唯一的问题是当您从紧凑模式调用 presentModalViewController 时, topLayoutGuide.length 172.0 。所以我不得不为这种情况硬编码一个值。

The only problem is when you're calling presentModalViewController from compact mode, topLayoutGuide.length is 172.0 for unknown reason. So I had to hardcode a value for that case.

这篇关于为什么顶部布局指南会在我的iMessage扩展中移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆