ios - 以半尺寸显示模态视图 Controller

标签 ios swift

我试图在另一个大小为一半父 View Controller 的 UIViewController 上实现呈现 modal view controller 为:

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

现在我需要在用户单击父 View Controller 时关闭我的半尺寸 View Controller 。
如何实现?

最佳答案

以下作品:(在运行 Swift 3、iOS 10.2 的 Xcode 8.3 中检查)

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处插入 View

关于ios - 以半尺寸显示模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288017/

相关文章:

ios - 在 Swift 中对 UIButton 使用 setTitle 时出现延迟

IOS iMessage扩展截图检测

ios - 在 Swift 上创建 UITableView 单元格

swift - 为什么我的 DateFormatter .date(from : String) return nil

c# - 如何在c# monotuch中打印多个页面的UITableview内容数据

ios - 配置文件状态无效(由 XCode 管理)

php - 无法在 iOS 中解析从 PHP 获取的 JSON

ios - Xcode 构建依赖于 Metro bundler

ios - 使用 UIScrollView 进行分页

java - Realm 图像存储互操作性 Android iOS (Swift/Java)