2019-01-21 23:04:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension UIViewController {
|
|
|
|
|
2020-10-09 09:26:54 +00:00
|
|
|
private enum UIViewControllerConstants {
|
|
|
|
static let fabButtonSize = CGSize(width: 78, height: 78)
|
|
|
|
static let fabButtonTrailingMargin: CGFloat = 0
|
|
|
|
static let fabButtonBottomMargin: CGFloat = 9
|
|
|
|
}
|
|
|
|
|
2019-01-21 23:04:34 +00:00
|
|
|
/// Remove back bar button title when pushing a view controller.
|
|
|
|
/// This method should be called on the previous controller in UINavigationController stack.
|
2020-05-05 07:17:47 +00:00
|
|
|
@objc func vc_removeBackTitle() {
|
2019-01-21 23:04:34 +00:00
|
|
|
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Add a child view controller matching current view controller view.
|
|
|
|
///
|
|
|
|
/// - Parameter viewController: The child view controller to add.
|
|
|
|
func vc_addChildViewController(viewController: UIViewController) {
|
|
|
|
self.vc_addChildViewController(viewController: viewController, onView: self.view)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Add a child view controller on current view controller.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - viewController: The child view controller to add.
|
|
|
|
/// - view: The view on which to add the child view controller view.
|
2021-12-23 13:08:00 +00:00
|
|
|
/// - animated: true to add a fade in animation
|
|
|
|
func vc_addChildViewController(viewController: UIViewController, onView view: UIView, animated: Bool = false) {
|
2019-02-20 15:45:32 +00:00
|
|
|
self.addChild(viewController)
|
2019-01-21 23:04:34 +00:00
|
|
|
|
|
|
|
viewController.view.frame = view.bounds
|
2021-12-23 13:08:00 +00:00
|
|
|
if animated {
|
|
|
|
viewController.view.alpha = 0
|
|
|
|
}
|
2019-01-22 16:09:15 +00:00
|
|
|
view.vc_addSubViewMatchingParent(viewController.view)
|
2021-12-23 13:08:00 +00:00
|
|
|
if animated {
|
|
|
|
UIView.animate(withDuration: 0.2) {
|
|
|
|
viewController.view.alpha = 1
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 15:45:32 +00:00
|
|
|
viewController.didMove(toParent: self)
|
2019-01-21 23:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Remove a child view controller from current view controller.
|
|
|
|
///
|
2021-12-23 13:08:00 +00:00
|
|
|
/// - Parameters:
|
|
|
|
/// - viewController: The child view controller to remove.
|
|
|
|
/// - animated: true to add a fade out animation
|
|
|
|
func vc_removeChildViewController(viewController: UIViewController, animated: Bool = false) {
|
2019-02-20 15:45:32 +00:00
|
|
|
viewController.willMove(toParent: nil)
|
2021-12-23 13:08:00 +00:00
|
|
|
if animated {
|
|
|
|
UIView.animate(withDuration: 0.2) {
|
|
|
|
viewController.view.alpha = 0
|
|
|
|
} completion: { finished in
|
|
|
|
viewController.view.removeFromSuperview()
|
|
|
|
viewController.view.alpha = 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
viewController.view.removeFromSuperview()
|
|
|
|
}
|
2019-02-20 15:45:32 +00:00
|
|
|
viewController.removeFromParent()
|
2019-01-21 23:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Remove current view controller from parent.
|
2021-12-23 13:08:00 +00:00
|
|
|
///
|
|
|
|
/// - Parameter animated: true to add a fade out animation
|
|
|
|
func vc_removeFromParent(animated: Bool = false) {
|
|
|
|
self.vc_removeChildViewController(viewController: self, animated: animated)
|
2019-01-21 23:04:34 +00:00
|
|
|
}
|
2020-10-09 09:26:54 +00:00
|
|
|
|
|
|
|
/// Adds a floating action button to the bottom-right of the page.
|
|
|
|
/// - Parameters:
|
|
|
|
/// - image: Image to be showed in fab
|
|
|
|
/// - target: target of the button
|
|
|
|
/// - action: action of the button
|
|
|
|
/// - Returns: The FAB view
|
|
|
|
@discardableResult
|
|
|
|
@objc func vc_addFAB(withImage image: UIImage,
|
|
|
|
target: Any?,
|
2020-10-09 09:38:09 +00:00
|
|
|
action: Selector?) -> UIImageView {
|
2020-10-09 09:26:54 +00:00
|
|
|
|
|
|
|
let fabImageView = UIImageView(image: image)
|
|
|
|
fabImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
fabImageView.backgroundColor = .clear
|
|
|
|
fabImageView.contentMode = .center
|
|
|
|
fabImageView.layer.shadowOpacity = 0.3
|
|
|
|
fabImageView.layer.shadowOffset = CGSize(width: 0, height: 3)
|
|
|
|
fabImageView.isUserInteractionEnabled = true
|
|
|
|
|
|
|
|
self.view.addSubview(fabImageView)
|
|
|
|
|
|
|
|
fabImageView.widthAnchor.constraint(equalToConstant: UIViewControllerConstants.fabButtonSize.width).isActive = true
|
|
|
|
fabImageView.heightAnchor.constraint(equalToConstant: UIViewControllerConstants.fabButtonSize.height).isActive = true
|
|
|
|
fabImageView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor,
|
|
|
|
constant: UIViewControllerConstants.fabButtonTrailingMargin).isActive = true
|
|
|
|
self.view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: fabImageView.bottomAnchor,
|
|
|
|
constant: UIViewControllerConstants.fabButtonBottomMargin).isActive = true
|
|
|
|
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: target, action: action)
|
|
|
|
tapGestureRecognizer.numberOfTouchesRequired = 1
|
|
|
|
tapGestureRecognizer.numberOfTapsRequired = 1
|
|
|
|
fabImageView.addGestureRecognizer(tapGestureRecognizer)
|
|
|
|
|
2020-10-09 09:38:09 +00:00
|
|
|
return fabImageView
|
2020-10-09 09:26:54 +00:00
|
|
|
}
|
2021-10-01 12:51:08 +00:00
|
|
|
|
|
|
|
/// Set leftBarButtonItem with split view display mode button if there is no leftBarButtonItem defined and splitViewController exists.
|
|
|
|
/// To be Used when view controller is displayed as detail controller in split view.
|
|
|
|
func vc_setupDisplayModeLeftBarButtonItemIfNeeded() {
|
|
|
|
guard let splitViewController = self.splitViewController, self.navigationItem.leftBarButtonItem == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no leftBarButtonItem defined,
|
|
|
|
// set split view display mode button as left bar button item
|
|
|
|
self.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
|
|
|
|
}
|
2019-01-21 23:04:34 +00:00
|
|
|
}
|