这是我的扩展程序:
extension UIViewController: UIViewControllerTransitioningDelegate {
func presentAssignBookToClassesViewController(controller: BWAssignBookToClassesViewController) {
controller.modalPresentationStyle = .Custom
controller.transitioningDelegate = self
controller.preferredContentSize = CGSizeMake(500, 575)
presentViewController(controller, animated: true, completion: nil)
}
func presentSettingsStoryboard() {
if let settingsController = UIStoryboard(name: "TeacherSettingsStoryboard", bundle: nil).instantiateInitialViewController() {
settingsController.modalPresentationStyle = .Custom
settingsController.transitioningDelegate = self
settingsController.preferredContentSize = CGSizeMake(500, 575)
presentViewController(settingsController, animated: true, completion: nil)
}
}
//MARK: - UIViewControllerTransitioningDelegate
public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
}
}
在presentationControllerForPresentedViewController:
内,我需要返回BWOverlayPresentationController
或BWSettingsPresentationController
,具体取决于调用的方法。怎么做到这一点?
答案 0 :(得分:1)
public func presentationControllerForPresentedViewController(presented: UIViewController?, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
// You can create some property in presented/presenting viewController.
// and check here to return specific viewContoller.
if (presented.(somePropertyInViewController)) {
return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
}
else {
return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting)
}
}
答案 1 :(得分:1)
我建议您可以使用两个viewController对象创建一个BaseViewContoller,例如:BWOverlayPresentationController,BWSettingsPresentationController,并根据条件返回特定的视图控制器。
(?<=#)\d+
答案 2 :(得分:1)
您可以通过restorationIdentifier
区分它们(您只需使用故事板设置):
//MARK: - UIViewControllerTransitioningDelegate
public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
if presented.restorationIdentifier == BWSettingsRestorationIdentifier {
return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting)
} else {
return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
}
}