Skip to content

Commit

Permalink
Merge pull request hyperoslo#475 from shalamowww/master
Browse files Browse the repository at this point in the history
Added galleryOnly configuration mode
  • Loading branch information
3lvis authored Jan 30, 2021
2 parents 4cfb7e3 + ba263f0 commit c0b3230
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 41 deletions.
7 changes: 7 additions & 0 deletions Source/BottomView/BottomContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Source/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
88 changes: 58 additions & 30 deletions Source/Extensions/ConstraintsSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *) {
Expand All @@ -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))
}
}

Expand Down
23 changes: 19 additions & 4 deletions Source/ImageGallery/ImageGalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
24 changes: 17 additions & 7 deletions Source/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -122,8 +132,6 @@ open class ImagePickerController: UIViewController {
view.backgroundColor = UIColor.white
view.backgroundColor = configuration.mainColor

cameraController.view.addGestureRecognizer(panGestureRecognizer)

subscribe()
setupConstraints()
}
Expand All @@ -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()

Expand Down

0 comments on commit c0b3230

Please sign in to comment.