Skip to content

Commit ba263f0

Browse files
committed
Added galleryOnly configuration mode
1 parent 4cfb7e3 commit ba263f0

File tree

5 files changed

+102
-41
lines changed

5 files changed

+102
-41
lines changed

Source/BottomView/BottomContainerView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ open class BottomContainerView: UIView {
8888
stackView.addGestureRecognizer(tapGestureRecognizer)
8989

9090
setupConstraints()
91+
if configuration.galleryOnly {
92+
borderPickerButton.isHidden = true
93+
pickerButton.isHidden = true
94+
}
95+
if !configuration.allowMultiplePhotoSelection {
96+
stackView.isHidden = true
97+
}
9198
}
9299

93100
// MARK: - Action methods

Source/Configuration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import UIKit
5353
@objc public var allowedOrientations = UIInterfaceOrientationMask.all
5454
@objc public var allowVolumeButtonsToTakePicture = true
5555
@objc public var useLowResolutionPreviewImage = false
56+
@objc public var galleryOnly = false
5657

5758
// MARK: Images
5859
@objc public var indicatorView: UIView = {

Source/Extensions/ConstraintsSetup.swift

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,65 @@ extension ImagePickerController {
110110
relatedBy: .equal, toItem: view, attribute: attribute,
111111
multiplier: 1, constant: 0))
112112
}
113-
114-
for attribute: NSLayoutConstraint.Attribute in [.left, .top, .width] {
115-
view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: attribute,
116-
relatedBy: .equal, toItem: view, attribute: attribute,
117-
multiplier: 1, constant: 0))
118-
}
119-
120-
for attribute in topViewAttributes {
121-
view.addConstraint(NSLayoutConstraint(item: topView, attribute: attribute,
122-
relatedBy: .equal, toItem: self.view, attribute: attribute,
123-
multiplier: 1, constant: 0))
124-
}
125-
126-
if #available(iOS 11.0, *) {
127-
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top,
128-
relatedBy: .equal, toItem: view.safeAreaLayoutGuide,
129-
attribute: .top,
130-
multiplier: 1, constant: 0))
113+
114+
if configuration.galleryOnly {
115+
116+
for attribute: NSLayoutConstraint.Attribute in [.left, .right] {
117+
view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: attribute,
118+
relatedBy: .equal, toItem: view, attribute: attribute,
119+
multiplier: 1, constant: 0))
120+
}
121+
let bottomHeightPadding: CGFloat
122+
if #available(iOS 11.0, *) {
123+
view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .top,
124+
relatedBy: .equal, toItem: view.safeAreaLayoutGuide,
125+
attribute: .top,
126+
multiplier: 1, constant: 0))
127+
bottomHeightPadding = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
128+
} else {
129+
view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .top,
130+
relatedBy: .equal, toItem: view,
131+
attribute: .top,
132+
multiplier: 1, constant: 0))
133+
bottomHeightPadding = 0
134+
}
135+
view.addConstraint(NSLayoutConstraint(item: galleryView, attribute: .height,
136+
relatedBy: .equal, toItem: view, attribute: .height,
137+
multiplier: 1, constant: -(BottomContainerView.Dimensions.height + bottomHeightPadding)))
138+
131139
} else {
132-
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top,
133-
relatedBy: .equal, toItem: view,
134-
attribute: .top,
135-
multiplier: 1, constant: 0))
140+
141+
for attribute: NSLayoutConstraint.Attribute in [.left, .top, .width] {
142+
view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: attribute,
143+
relatedBy: .equal, toItem: view, attribute: attribute,
144+
multiplier: 1, constant: 0))
145+
}
146+
147+
for attribute in topViewAttributes {
148+
view.addConstraint(NSLayoutConstraint(item: topView, attribute: attribute,
149+
relatedBy: .equal, toItem: self.view, attribute: attribute,
150+
multiplier: 1, constant: 0))
151+
}
152+
153+
if #available(iOS 11.0, *) {
154+
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top,
155+
relatedBy: .equal, toItem: view.safeAreaLayoutGuide,
156+
attribute: .top,
157+
multiplier: 1, constant: 0))
158+
} else {
159+
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .top,
160+
relatedBy: .equal, toItem: view,
161+
attribute: .top,
162+
multiplier: 1, constant: 0))
163+
}
164+
165+
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .height,
166+
relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,
167+
multiplier: 1, constant: TopView.Dimensions.height))
168+
169+
view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: .height,
170+
relatedBy: .equal, toItem: view, attribute: .height,
171+
multiplier: 1, constant: -BottomContainerView.Dimensions.height))
136172
}
137173

138174
if #available(iOS 11.0, *) {
@@ -149,14 +185,6 @@ extension ImagePickerController {
149185
multiplier: 1,
150186
constant: BottomContainerView.Dimensions.height))
151187
}
152-
153-
view.addConstraint(NSLayoutConstraint(item: topView, attribute: .height,
154-
relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,
155-
multiplier: 1, constant: TopView.Dimensions.height))
156-
157-
view.addConstraint(NSLayoutConstraint(item: cameraController.view!, attribute: .height,
158-
relatedBy: .equal, toItem: view, attribute: .height,
159-
multiplier: 1, constant: -BottomContainerView.Dimensions.height))
160188
}
161189
}
162190

Source/ImageGallery/ImageGalleryView.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class ImageGalleryView: UIView {
4242

4343
lazy var collectionViewLayout: UICollectionViewLayout = { [unowned self] in
4444
let layout = ImageGalleryLayout(configuration: self.configuration)
45-
layout.scrollDirection = .horizontal
45+
layout.scrollDirection = configuration.galleryOnly ? .vertical : .horizontal
4646
layout.minimumInteritemSpacing = self.configuration.cellSpacing
4747
layout.minimumLineSpacing = 2
4848
layout.sectionInset = UIEdgeInsets.zero
@@ -113,7 +113,11 @@ open class ImageGalleryView: UIView {
113113
collectionView.register(ImageGalleryViewCell.self,
114114
forCellWithReuseIdentifier: CollectionView.reusableIdentifier)
115115

116-
[collectionView, topSeparator].forEach { addSubview($0) }
116+
if configuration.galleryOnly {
117+
addSubview(collectionView)
118+
} else {
119+
[collectionView, topSeparator].forEach { addSubview($0) }
120+
}
117121

118122
topSeparator.addSubview(configuration.indicatorView)
119123

@@ -136,8 +140,19 @@ open class ImageGalleryView: UIView {
136140
topSeparator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleWidth]
137141
configuration.indicatorView.frame = CGRect(x: (totalWidth - configuration.indicatorWidth) / 2, y: (topSeparator.frame.height - configuration.indicatorHeight) / 2,
138142
width: configuration.indicatorWidth, height: configuration.indicatorHeight)
139-
collectionView.frame = CGRect(x: 0, y: topSeparator.frame.height, width: totalWidth, height: collectionFrame - topSeparator.frame.height)
140-
collectionSize = CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
143+
144+
collectionView.frame = CGRect(x: 0,
145+
y: topSeparator.superview != nil ? topSeparator.frame.height : 0,
146+
width: totalWidth,
147+
height: collectionFrame - topSeparator.frame.height)
148+
149+
if configuration.galleryOnly {
150+
let cellSize = collectionView.bounds.width/3 - self.configuration.cellSpacing*2
151+
collectionSize = CGSize(width: cellSize, height: cellSize)
152+
} else {
153+
collectionSize = CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
154+
}
155+
141156
noImagesLabel.center = CGPoint(x: bounds.width / 2, y: (bounds.height + Dimensions.galleryBarHeight) / 2)
142157

143158
collectionView.reloadData()

Source/ImagePickerController.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,20 @@ open class ImagePickerController: UIViewController {
110110

111111
open override func viewDidLoad() {
112112
super.viewDidLoad()
113-
114-
for subview in [cameraController.view, galleryView, bottomContainer, topView] {
115-
view.addSubview(subview!)
116-
subview?.translatesAutoresizingMaskIntoConstraints = false
113+
114+
let addSubview: (UIView) -> Void = { subview in
115+
self.view.addSubview(subview)
116+
subview.translatesAutoresizingMaskIntoConstraints = false
117+
}
118+
119+
if !configuration.galleryOnly {
120+
addSubview(cameraController.view)
121+
addSubview(topView)
122+
cameraController.view.addGestureRecognizer(panGestureRecognizer)
123+
}
124+
125+
for subview in [galleryView, bottomContainer] {
126+
addSubview(subview)
117127
}
118128

119129
view.addSubview(volumeView)
@@ -122,8 +132,6 @@ open class ImagePickerController: UIViewController {
122132
view.backgroundColor = UIColor.white
123133
view.backgroundColor = configuration.mainColor
124134

125-
cameraController.view.addGestureRecognizer(panGestureRecognizer)
126-
127135
subscribe()
128136
setupConstraints()
129137
}
@@ -149,10 +157,12 @@ open class ImagePickerController: UIViewController {
149157
galleryView.collectionView.transform = CGAffineTransform.identity
150158
galleryView.collectionView.contentInset = UIEdgeInsets.zero
151159

152-
galleryView.frame = CGRect(x: 0,
160+
if !configuration.galleryOnly {
161+
galleryView.frame = CGRect(x: 0,
153162
y: totalSize.height - bottomContainer.frame.height - galleryHeight,
154163
width: totalSize.width,
155164
height: galleryHeight)
165+
}
156166
galleryView.updateFrames()
157167
checkStatus()
158168

0 commit comments

Comments
 (0)