Skip to content

Commit 4fc24cf

Browse files
Merge pull request #46 from componentskit/fix/ukmodal
Fix bug with size in UKModalController
2 parents 823a8fa + 471148d commit 4fc24cf

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

Sources/ComponentsKit/Components/Modal/UIKit/UKModalController.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ open class UKModalController<VM: ModalVM>: UIViewController {
1414
/// A model that defines the appearance properties.
1515
public let model: VM
1616

17+
private var containerWidthConstraint: NSLayoutConstraint?
18+
19+
// MARK: - Subviews
20+
1721
/// The optional header view of the modal.
1822
public var header: UIView?
1923
/// The main body view of the modal.
@@ -154,14 +158,36 @@ open class UKModalController<VM: ModalVM>: UIViewController {
154158
greaterThanOrEqualToConstant: 80
155159
).isActive = true
156160

157-
let containerWidthConstraint = self.container.width(self.model.size.maxWidth).width
158-
containerWidthConstraint?.priority = .defaultHigh
161+
self.containerWidthConstraint = self.container.width(self.model.size.maxWidth).width
162+
self.containerWidthConstraint?.priority = .defaultHigh
159163

160-
let bodyWrapperWidthConstraint = self.bodyWrapper.width(self.model.size.maxWidth).width
161-
bodyWrapperWidthConstraint?.priority = .defaultHigh
164+
self.bodyWrapper.widthAnchor.constraint(equalTo: self.container.widthAnchor).isActive = true
162165

163166
self.container.centerHorizontally()
164167
}
168+
169+
open override func viewWillTransition(
170+
to size: CGSize,
171+
with coordinator: any UIViewControllerTransitionCoordinator
172+
) {
173+
self.containerWidthConstraint?.isActive = false
174+
super.viewWillTransition(to: size, with: coordinator)
175+
}
176+
177+
open override func viewDidLayoutSubviews() {
178+
super.viewDidLayoutSubviews()
179+
180+
let availableWidth = self.view.bounds.width
181+
let requiredWidth = self.model.size.maxWidth
182+
+ self.model.outerPaddings.leading
183+
+ self.model.outerPaddings.trailing
184+
if availableWidth > requiredWidth {
185+
self.containerWidthConstraint?.priority = .required
186+
} else {
187+
self.containerWidthConstraint?.priority = .defaultHigh
188+
}
189+
self.containerWidthConstraint?.isActive = true
190+
}
165191
}
166192

167193
// MARK: - Style Helpers

0 commit comments

Comments
 (0)