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

时间:2016-08-22 00:38:57

标签: ios swift autolayout imessage

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

在我的主viewController中:

topLayoutGuide.length

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

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

3 个答案:

答案 0 :(得分:7)

作为一种解决方法,我使用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因{1}}原因未知。所以我不得不为这种情况硬编码一个值。

答案 1 :(得分:1)

我相信这是以前iOS 10测试版中的已知错误。在我将iOS版本升级到最新版本之后,我遇到了同样的问题,顶部和底部布局指南的工作正如我所料。

答案 2 :(得分:0)

我使用了稍微不同版本的安德烈

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

extension MyViewController: UIViewControllerTransitioningDelegate {
    func presentationController(
         forPresented presented: UIViewController,
         presenting: UIViewController?,
         source: UIViewController
    ) -> UIPresentationController? {
         let vc = PresentationController(
             presentedViewController: presented,
             presenting: presenting
         )
         vc.framePresented = modalBoundaries.frame
         return vc
    }
}

class PresentationController: UIPresentationController {
     var framePresented = CGRect.zero
     override var frameOfPresentedViewInContainerView: CGRect {
         return framePresented
    }
}

modalBoundaries是一个虚拟UIView约束(在我的情况下通过XIB),以尊重任何TopLayoutGuide长度。