Created
October 7, 2016 09:26
-
-
Save akovalov/6784e526ee273decefa7a21312066a1a to your computer and use it in GitHub Desktop.
Toolbar customization for TOCropViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CropViewController.swift | |
// | |
// | |
// Created by Alex Kovalov on 10/6/16. | |
// Copyright © 2016 . All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import TOCropViewController | |
class CropViewController: UIViewController { | |
// MARK: Properties | |
@IBOutlet weak var toolbarView: UIView! | |
@IBOutlet weak var backButton: UIButton! | |
@IBOutlet weak var rotateCounterClockwiseButton: UIButton! | |
@IBOutlet weak var resetLayoutButton: UIButton! | |
@IBOutlet weak var rotateClockwiseButton: UIButton! | |
@IBOutlet weak var nextButton: UIButton! | |
var cropVC: CustomTOCropViewController! | |
override var preferredStatusBarStyle: UIStatusBarStyle { | |
return .lightContent | |
} | |
// MARK: Lifecycle | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
cropVC.view.frame = CGRect(x: 0, y: 20, width: view.bounds.size.width, height: view.bounds.size.height - toolbarView.frame.size.height - 20) | |
cropVC.view.setNeedsLayout() | |
} | |
// MARK: Actions | |
func setupWith(image: UIImage) { | |
cropVC = CustomTOCropViewController(image: image) | |
cropVC.delegate = self | |
cropVC.toolbar.isHidden = true | |
addChildViewController(cropVC) | |
view.addSubview(cropVC.view) | |
cropVC.didMove(toParentViewController: self) | |
view.bringSubview(toFront: toolbarView) | |
view.backgroundColor = cropVC.view.backgroundColor | |
resetLayout(resetLayoutButton) | |
} | |
@IBAction func next(_ sender: AnyObject) { | |
let selector = NSSelectorFromString("doneButtonTapped") | |
if cropVC.responds(to: selector) { | |
cropVC.perform(selector) | |
} | |
} | |
@IBAction func rotateClockwise(_ sender: AnyObject) { | |
let selector = NSSelectorFromString("rotateCropViewClockwise") | |
if cropVC.responds(to: selector) { | |
cropVC.perform(selector) | |
} | |
} | |
@IBAction func resetLayout(_ sender: AnyObject) { | |
let selector = NSSelectorFromString("resetCropViewLayout") | |
if cropVC.responds(to: selector) { | |
cropVC.perform(selector) | |
} | |
} | |
@IBAction func rotateCounterClockwise(_ sender: AnyObject) { | |
let selector = NSSelectorFromString("rotateCropViewCounterclockwise") | |
if cropVC.responds(to: selector) { | |
cropVC.perform(selector) | |
} | |
} | |
@IBAction func back(_ sender: AnyObject) { | |
_ = navigationController?.popViewController(animated: true) | |
} | |
} | |
extension CropViewController: TOCropViewControllerDelegate { | |
func cropViewController(_ cropViewController: TOCropViewController!, didCropTo image: UIImage!, with cropRect: CGRect, angle: Int) { | |
// TODO: go to next screen | |
} | |
} | |
class CustomTOCropViewController: TOCropViewController { | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
cropView.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CropViewController")
vc.setupWith(image: image)
self.navigationController?.pushViewController(vc, animated: true)