我有一个iMessage扩展名,并且顶部布局指南存在一些问题。我有一个MSMessagesAppViewController
,用于处理表示样式之间的更改。在我的扩展程序中,我有一个按钮。单击它时,我过渡到扩展的表示样式,然后以模态形式显示 View Controller 。这是问题所在:第二个VC中的UI隐藏在顶部导航栏的后面。当我将约束设置在顶部布局指南上时,我认为这很奇怪。因此,我仔细研究了代码,然后开始调试顶级布局指南。我注意到,在过渡到扩展的表示样式后,topLayoutGuide.length
=86。这应该是这样。但是,当我以模态形式显示第二个 View Controller 时,顶部布局指南会重置为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
}
在另一个以模态表示的 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
点移动模态视图 Controller :
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
。所以我不得不为这种情况硬编码一个值。