2015-07-08 9 views
4

Я пытался реализовать представление modal view controller на других UIViewController размере до половины контроллера представления родительском как:Present контроллер представления модальна в половинном размере

class ViewController: UIViewController, UIViewControllerTransitioningDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    @IBAction func tap(sender: AnyObject) { 
     var storyboard = UIStoryboard(name: "Main", bundle: nil) 
     var pvc = storyboard.instantiateViewControllerWithIdentifier("CustomTableViewController") as UITableViewController 

     pvc.modalPresentationStyle = UIModalPresentationStyle.Custom 
     pvc.transitioningDelegate = self 
     pvc.view.backgroundColor = UIColor.redColor() 

     self.presentViewController(pvc, animated: true, completion: nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { 
     return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presenting) 
    } 
} 

class HalfSizePresentationController : UIPresentationController { 
    override func frameOfPresentedViewInContainerView() -> CGRect { 
     return CGRect(x: 0, y: 0, width: containerView.bounds.width, height: containerView.bounds.height/2) 
    } 
} 

Теперь мне нужно, чтобы закрыть мою половину размера контроллера представления, когда пользователь нажимает родитель просмотрите контроллер.
Как достичь этого?

+0

Почему я пытался отобразить половину размера всплывающего контроллер представления по-твоему, но не сработал? – TomSawyer

ответ

1

Вам просто нужно добавить UIView к своему HalfSizePresentationController (возможно, затемнение). Затем добавить водопроводная жест распознаватель с этой точки зрения для обработки события крана, который будет вызван, чтобы закрыть вид ПРЕДСТАВЛЕНИЕ:

func dimmingViewTapped(tapRecognizer: UITapGestureRecognizer) { 
     presentingViewController.dismissViewControllerAnimated(true, completion: nil) 
    } 

Может быть, это поможет: http://zappdesigntemplates.com/custom-presentations-using-uipresentationcontroller-swift/

1

Я знаю его слишком поздно. Но ниже это самый простой ответ для достижения этого.

self.view.backgroundColor = UIColor.clearColor() 
self.datePicker.backgroundColor = UIColor.whiteColor() 
self.modalTransitionStyle = .CoverVertical 
self.modalPresentationStyle = .OverCurrentContext 

, где сам контроллер представлений представлен.

+0

Если я правильно понял, я пробовал это, но не '.overCurrentContext' разрушает всю пользовательскую презентацию, как и для меня? – Natanel

0

следующих работ: (Проверено в Xcode 8.3 работает Swift 3, IOS 10,2)

class HalfSizePresentationController: UIPresentationController { 

let backgroundView = UIView() 

override var frameOfPresentedViewInContainerView: CGRect { 
    // your implementation 
} 

override func presentationTransitionDidEnd(_ completed: Bool) { 
    if completed { 
     backgroundView.frame = (containerView?.frame)! 
     let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddMenuPresentationController.dismissPVC(_:))) 
     backgroundView.addGestureRecognizer(tapGestureRecognizer) 
     containerView?.insertSubview(backgroundView, at: 0) 
    } 
} 

override func dismissalTransitionDidEnd(_ completed: Bool) { 
    if completed { 
     backgroundView.removeFromSuperview() 
    } 
} 

func dismissPVC(_ gestureRecognizer: UIGestureRecognizer) { 
    self.presentedViewController.dismiss(animated: true, completion: nil) 
} 
} 

не забудьте вставить вид с индексом 0

Смежные вопросы