From ba263f0a6d4d10f30204bc496265dcc2bd1be18b Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 27 Dec 2020 20:50:16 +0300 Subject: [PATCH] Added galleryOnly configuration mode --- Source/BottomView/BottomContainerView.swift | 7 ++ Source/Configuration.swift | 1 + Source/Extensions/ConstraintsSetup.swift | 88 ++++++++++++++------- Source/ImageGallery/ImageGalleryView.swift | 23 +++++- Source/ImagePickerController.swift | 24 ++++-- 5 files changed, 102 insertions(+), 41 deletions(-) diff --git a/Source/BottomView/BottomContainerView.swift b/Source/BottomView/BottomContainerView.swift index 58e1b1fe..1133cf9b 100644 --- a/Source/BottomView/BottomContainerView.swift +++ b/Source/BottomView/BottomContainerView.swift @@ -88,6 +88,13 @@ open class BottomContainerView: UIView { stackView.addGestureRecognizer(tapGestureRecognizer) setupConstraints() + if configuration.galleryOnly { + borderPickerButton.isHidden = true + pickerButton.isHidden = true + } + if !configuration.allowMultiplePhotoSelection { + stackView.isHidden = true + } } // MARK: - Action methods diff --git a/Source/Configuration.swift b/Source/Configuration.swift index e23f7b08..0928768c 100644 --- a/Source/Configuration.swift +++ b/Source/Configuration.swift @@ -53,6 +53,7 @@ import UIKit @objc public var allowedOrientations = UIInterfaceOrientationMask.all @objc public var allowVolumeButtonsToTakePicture = true @objc public var useLowResolutionPreviewImage = false + @objc public var galleryOnly = false // MARK: Images @objc public var indicatorView: UIView = { diff --git a/Source/Extensions/ConstraintsSetup.swift b/Source/Extensions/ConstraintsSetup.swift index 119877fe..66829a70 100644 --- a/Source/Extensions/ConstraintsSetup.swift +++ b/Source/Extensions/ConstraintsSetup.swift @@ -110,29 +110,65 @@ extension ImagePickerController { relatedBy: .equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0)) } - - for attribute: NSLayoutConstraint.Attribute in [.left, .top, .width] { - view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: attribute, - relatedBy: .equal, toItem: view, attribute: attribute, - multiplier: 1, constant: 0)) - } - - for attribute in topViewAttributes { - view.addConstraint(NSLayoutConstraint(item: topView, attribute: attribute, - relatedBy: .equal, toItem: self.view, attribute: attribute, - multiplier: 1, constant: 0)) - } - - if #available(iOS 11.0, *) { - view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top, - relatedBy: .equal, toItem: view.safeAreaLayoutGuide, - attribute: .top, - multiplier: 1, constant: 0)) + + if configuration.galleryOnly { + + for attribute: NSLayoutConstraint.Attribute in [.left, .right] { + view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: attribute, + relatedBy: .equal, toItem: view, attribute: attribute, + multiplier: 1, constant: 0)) + } + let bottomHeightPadding: CGFloat + if #available(iOS 11.0, *) { + view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .top, + relatedBy: .equal, toItem: view.safeAreaLayoutGuide, + attribute: .top, + multiplier: 1, constant: 0)) + bottomHeightPadding = UIApplication.shared.keyWindow!.safeAreaInsets.bottom + } else { + view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .top, + relatedBy: .equal, toItem: view, + attribute: .top, + multiplier: 1, constant: 0)) + bottomHeightPadding = 0 + } + view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .height, + relatedBy: .equal, toItem: view, attribute: .height, + multiplier: 1, constant: -(BottomContainerView.Dimensions.height + bottomHeightPadding))) + } else { - view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top, - relatedBy: .equal, toItem: view, - attribute: .top, - multiplier: 1, constant: 0)) + + for attribute: NSLayoutConstraint.Attribute in [.left, .top, .width] { + view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: attribute, + relatedBy: .equal, toItem: view, attribute: attribute, + multiplier: 1, constant: 0)) + } + + for attribute in topViewAttributes { + view.addConstraint(NSLayoutConstraint(item: topView, attribute: attribute, + relatedBy: .equal, toItem: self.view, attribute: attribute, + multiplier: 1, constant: 0)) + } + + if #available(iOS 11.0, *) { + view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top, + relatedBy: .equal, toItem: view.safeAreaLayoutGuide, + attribute: .top, + multiplier: 1, constant: 0)) + } else { + view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top, + relatedBy: .equal, toItem: view, + attribute: .top, + multiplier: 1, constant: 0)) + } + + view.addConstraint(NSLayoutConstraint(item: topView, attribute: .height, + relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, + multiplier: 1, constant: TopView.Dimensions.height)) + + view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: .height, + relatedBy: .equal, toItem: view, attribute: .height, + multiplier: 1, constant: -BottomContainerView.Dimensions.height)) } if #available(iOS 11.0, *) { @@ -149,14 +185,6 @@ extension ImagePickerController { multiplier: 1, constant: BottomContainerView.Dimensions.height)) } - - view.addConstraint(NSLayoutConstraint(item: topView, attribute: .height, - relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, - multiplier: 1, constant: TopView.Dimensions.height)) - - view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: .height, - relatedBy: .equal, toItem: view, attribute: .height, - multiplier: 1, constant: -BottomContainerView.Dimensions.height)) } } diff --git a/Source/ImageGallery/ImageGalleryView.swift b/Source/ImageGallery/ImageGalleryView.swift index 6632e2ae..27eb31ca 100644 --- a/Source/ImageGallery/ImageGalleryView.swift +++ b/Source/ImageGallery/ImageGalleryView.swift @@ -42,7 +42,7 @@ open class ImageGalleryView: UIView { lazy var collectionViewLayout: UICollectionViewLayout = { [unowned self] in let layout = ImageGalleryLayout(configuration: self.configuration) - layout.scrollDirection = .horizontal + layout.scrollDirection = configuration.galleryOnly ? .vertical : .horizontal layout.minimumInteritemSpacing = self.configuration.cellSpacing layout.minimumLineSpacing = 2 layout.sectionInset = UIEdgeInsets.zero @@ -113,7 +113,11 @@ open class ImageGalleryView: UIView { collectionView.register(ImageGalleryViewCell.self, forCellWithReuseIdentifier: CollectionView.reusableIdentifier) - [collectionView, topSeparator].forEach { addSubview($0) } + if configuration.galleryOnly { + addSubview(collectionView) + } else { + [collectionView, topSeparator].forEach { addSubview($0) } + } topSeparator.addSubview(configuration.indicatorView) @@ -136,8 +140,19 @@ open class ImageGalleryView: UIView { topSeparator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleWidth] configuration.indicatorView.frame = CGRect(x: (totalWidth - configuration.indicatorWidth) / 2, y: (topSeparator.frame.height - configuration.indicatorHeight) / 2, width: configuration.indicatorWidth, height: configuration.indicatorHeight) - collectionView.frame = CGRect(x: 0, y: topSeparator.frame.height, width: totalWidth, height: collectionFrame - topSeparator.frame.height) - collectionSize = CGSize(width: collectionView.frame.height, height: collectionView.frame.height) + + collectionView.frame = CGRect(x: 0, + y: topSeparator.superview != nil ? topSeparator.frame.height : 0, + width: totalWidth, + height: collectionFrame - topSeparator.frame.height) + + if configuration.galleryOnly { + let cellSize = collectionView.bounds.width/3 - self.configuration.cellSpacing*2 + collectionSize = CGSize(width: cellSize, height: cellSize) + } else { + collectionSize = CGSize(width: collectionView.frame.height, height: collectionView.frame.height) + } + noImagesLabel.center = CGPoint(x: bounds.width / 2, y: (bounds.height + Dimensions.galleryBarHeight) / 2) collectionView.reloadData() diff --git a/Source/ImagePickerController.swift b/Source/ImagePickerController.swift index 6ec3158b..5ccf6754 100644 --- a/Source/ImagePickerController.swift +++ b/Source/ImagePickerController.swift @@ -110,10 +110,20 @@ open class ImagePickerController: UIViewController { open override func viewDidLoad() { super.viewDidLoad() - - for subview in [cameraController.view, galleryView, bottomContainer, topView] { - view.addSubview(subview!) - subview?.translatesAutoresizingMaskIntoConstraints = false + + let addSubview: (UIView) -> Void = { subview in + self.view.addSubview(subview) + subview.translatesAutoresizingMaskIntoConstraints = false + } + + if !configuration.galleryOnly { + addSubview(cameraController.view) + addSubview(topView) + cameraController.view.addGestureRecognizer(panGestureRecognizer) + } + + for subview in [galleryView, bottomContainer] { + addSubview(subview) } view.addSubview(volumeView) @@ -122,8 +132,6 @@ open class ImagePickerController: UIViewController { view.backgroundColor = UIColor.white view.backgroundColor = configuration.mainColor - cameraController.view.addGestureRecognizer(panGestureRecognizer) - subscribe() setupConstraints() } @@ -149,10 +157,12 @@ open class ImagePickerController: UIViewController { galleryView.collectionView.transform = CGAffineTransform.identity galleryView.collectionView.contentInset = UIEdgeInsets.zero - galleryView.frame = CGRect(x: 0, + if !configuration.galleryOnly { + galleryView.frame = CGRect(x: 0, y: totalSize.height - bottomContainer.frame.height - galleryHeight, width: totalSize.width, height: galleryHeight) + } galleryView.updateFrames() checkStatus()