|
| 1 | +// |
| 2 | +// ClippedContainerViewController.swift |
| 3 | +// ThingsUI |
| 4 | +// |
| 5 | +// Created by Ryan Nystrom on 3/10/18. |
| 6 | +// Copyright © 2018 Ryan Nystrom. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +internal class ClippedContainerViewController: UIViewController { |
| 12 | + |
| 13 | + private let options: ContextMenu.Options |
| 14 | + private let containedViewController: UINavigationController |
| 15 | + |
| 16 | + init(options: ContextMenu.Options, viewController: UIViewController) { |
| 17 | + self.options = options |
| 18 | + self.containedViewController = UINavigationController(rootViewController: viewController) |
| 19 | + super.init(nibName: nil, bundle: nil) |
| 20 | + } |
| 21 | + |
| 22 | + required init?(coder aDecoder: NSCoder) { |
| 23 | + fatalError("init(coder:) has not been implemented") |
| 24 | + } |
| 25 | + |
| 26 | + override func viewDidLoad() { |
| 27 | + super.viewDidLoad() |
| 28 | + view.layer.cornerRadius = options.containerStyle.cornerRadius |
| 29 | + view.layer.shadowRadius = options.containerStyle.shadowRadius |
| 30 | + view.layer.shadowColor = UIColor.black.cgColor |
| 31 | + view.layer.shadowOpacity = 0.2 |
| 32 | + |
| 33 | + containedViewController.view.layer.cornerRadius = view.layer.cornerRadius |
| 34 | + containedViewController.view.clipsToBounds = true |
| 35 | + containedViewController.setNavigationBarHidden(options.menuStyle == .minimal, animated: false) |
| 36 | + |
| 37 | + let size = CGSize(width: 1, height: 1) |
| 38 | + UIGraphicsBeginImageContext(size) |
| 39 | + defer { UIGraphicsEndImageContext() } |
| 40 | + |
| 41 | + options.containerStyle.backgroundColor.setFill() |
| 42 | + UIBezierPath(rect: CGRect(origin: .zero, size: size)).fill() |
| 43 | + |
| 44 | + let image = UIGraphicsGetImageFromCurrentImageContext() |
| 45 | + let navigationBar = containedViewController.navigationBar |
| 46 | + navigationBar.isTranslucent = false |
| 47 | + navigationBar.setBackgroundImage(image, for: .any, barMetrics: .default) |
| 48 | + navigationBar.shadowImage = image |
| 49 | + |
| 50 | + addChildViewController(containedViewController) |
| 51 | + view.addSubview(containedViewController.view) |
| 52 | + containedViewController.didMove(toParentViewController: self) |
| 53 | + |
| 54 | + preferredContentSize = containedViewController.preferredContentSize |
| 55 | + } |
| 56 | + |
| 57 | + override func viewWillLayoutSubviews() { |
| 58 | + super.viewWillLayoutSubviews() |
| 59 | + containedViewController.view.frame = view.bounds |
| 60 | + } |
| 61 | + |
| 62 | + override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) { |
| 63 | + super.preferredContentSizeDidChange(forChildContentContainer: container) |
| 64 | + preferredContentSize = container.preferredContentSize |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments