ios – 为什么顶部布局指南在我的iMessage扩展中移动

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

在我的主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
}

在另一个模态呈现的视图控制器中:

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点移动模态视图控制器:
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,原因不明.所以我不得不为这种情况硬编码一个值.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...