-
Notifications
You must be signed in to change notification settings - Fork 51
/
ClippedContainerViewController.swift
100 lines (77 loc) · 3.8 KB
/
ClippedContainerViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// ClippedContainerViewController.swift
// ThingsUI
//
// Created by Ryan Nystrom on 3/10/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
class ClippedContainerViewController: UIViewController {
let viewController: UIViewController
private let options: ContextMenu.Options
private let containedViewController: ModalNavigationViewController
override var preferredStatusBarStyle: UIStatusBarStyle {
return viewController.preferredStatusBarStyle
}
init(options: ContextMenu.Options, viewController: UIViewController) {
self.viewController = viewController
self.options = options
self.containedViewController = ModalNavigationViewController(rootViewController: viewController)
super.init(nibName: nil, bundle: nil)
self.containedViewController.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.layer.cornerRadius = options.containerStyle.cornerRadius
view.layer.shadowRadius = options.containerStyle.shadowRadius
view.layer.shadowOpacity = options.containerStyle.shadowOpacity
view.layer.shadowOffset = options.containerStyle.shadowOffset
view.layer.shadowColor = UIColor.black.cgColor
view.backgroundColor = options.containerStyle.backgroundColor
if options.containerStyle.motionEffect && UIAccessibility.isReduceMotionEnabled == false {
let amount = 12
let tiltX = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
tiltX.minimumRelativeValue = -amount
tiltX.maximumRelativeValue = amount
let tiltY = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
tiltY.minimumRelativeValue = -amount
tiltY.maximumRelativeValue = amount
let group = UIMotionEffectGroup()
group.motionEffects = [tiltX, tiltY]
view.addMotionEffect(group)
}
containedViewController.view.layer.cornerRadius = view.layer.cornerRadius
containedViewController.view.clipsToBounds = true
containedViewController.setNavigationBarHidden(options.menuStyle == .minimal, animated: false)
let size = CGSize(width: 1, height: 1)
UIGraphicsBeginImageContext(size)
defer { UIGraphicsEndImageContext() }
options.containerStyle.backgroundColor.setFill()
UIBezierPath(rect: CGRect(origin: .zero, size: size)).fill()
let image = UIGraphicsGetImageFromCurrentImageContext()
let navigationBar = containedViewController.navigationBar
navigationBar.isTranslucent = false
navigationBar.setBackgroundImage(image, for: .any, barMetrics: .default)
navigationBar.shadowImage = image
addChild(containedViewController)
view.addSubview(containedViewController.view)
containedViewController.didMove(toParent: self)
preferredContentSize = containedViewController.preferredContentSize
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
containedViewController.view.frame = view.bounds
}
override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
super.preferredContentSizeDidChange(forChildContentContainer: container)
preferredContentSize = container.preferredContentSize
}
}
extension ClippedContainerViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.view.backgroundColor = options.containerStyle.backgroundColor
}
}