88
99import UIKit
1010
11- public class DJSemiModalViewController : UIViewController {
11+ open class DJSemiModalViewController : UIViewController {
1212
1313 // MARK: Private Properties
1414
@@ -74,6 +74,11 @@ public class DJSemiModalViewController: UIViewController {
7474
7575 public typealias ViewWillDismiss = ( ) -> Void
7676
77+ /**
78+ * Adjust content height automatically
79+ */
80+ public var automaticallyAdjustsContentHeight : Bool = false
81+
7782 /**
7883 * The title label for the view
7984 */
@@ -174,7 +179,7 @@ public class DJSemiModalViewController: UIViewController {
174179 scrollView. trailingAnchor. constraint ( equalTo: contentView. trailingAnchor, constant: - 24 ) ,
175180 scrollView. bottomAnchor. constraint ( equalTo: closeButton. topAnchor, constant: - 24 ) ,
176181 scrollViewHeightLayoutConstraint
177- ] )
182+ ] )
178183 }
179184
180185 private func setupStackView( ) {
@@ -236,11 +241,11 @@ public class DJSemiModalViewController: UIViewController {
236241 setupGestureRecognizer ( )
237242 }
238243
239- public override func viewDidLoad( ) {
244+ open override func viewDidLoad( ) {
240245 super. viewDidLoad ( )
241246 }
242247
243- public override func viewDidLayoutSubviews( ) {
248+ open override func viewDidLayoutSubviews( ) {
244249 super. viewDidLayoutSubviews ( )
245250
246251 updateScrollViewHeightConstraint ( )
@@ -261,9 +266,16 @@ extension DJSemiModalViewController {
261266 dismiss ( animated: true , completion: nil )
262267 }
263268
264- private func updateScrollViewHeightConstraint( ) {
265- scrollViewHeightLayoutConstraint. constant = stackView. frame. height
269+ public func updateScrollViewHeightConstraint( ) {
270+ let size = stackView. systemLayoutSizeFitting ( UILayoutFittingCompressedSize)
271+ scrollViewHeightLayoutConstraint. constant = automaticallyAdjustsContentHeight ? size. height : stackView. frame. height
266272 scrollViewHeightLayoutConstraint. isActive = scrollViewHeightLayoutConstraint. constant > 0.0
273+
274+ view. setNeedsUpdateConstraints ( )
275+
276+ UIView . animate ( withDuration: 0.25 , animations: {
277+ self . view. layoutIfNeeded ( )
278+ } )
267279 }
268280
269281 private func flashScrollViewScrollIndicatorsIfNeeded( ) {
@@ -283,7 +295,7 @@ extension DJSemiModalViewController {
283295 /**
284296 * Set the title and title label text
285297 */
286- override public var title : String ? {
298+ override open var title : String ? {
287299 didSet {
288300 self . titleLabel. text = title
289301 }
@@ -304,7 +316,7 @@ extension DJSemiModalViewController {
304316
305317 NSLayoutConstraint . activate ( [
306318 view. heightAnchor. constraint ( equalToConstant: height)
307- ] )
319+ ] )
308320
309321 addArrangedSubview ( view: view)
310322 }
@@ -431,7 +443,7 @@ extension DJSemiModalViewController {
431443
432444 // MARK: View Transition
433445
434- public override func viewWillTransition( to size: CGSize , with coordinator: UIViewControllerTransitionCoordinator ) {
446+ open override func viewWillTransition( to size: CGSize , with coordinator: UIViewControllerTransitionCoordinator ) {
435447 super. viewWillTransition ( to: size, with: coordinator)
436448 contentViewMaximumHeightLayoutConstraint. constant = size. height - 20
437449
0 commit comments