Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Enim habitant laoreet inceptos scelerisque senectus, tellus molestie ut. Eros ri
HStack {
Text(self.headerTitle)
.font(self.headerFont.font)
Spacer()
}
} else {
EmptyView()
Expand All @@ -211,6 +212,7 @@ Enim habitant laoreet inceptos scelerisque senectus, tellus molestie ut. Eros ri
}
}
.font(self.bodyFont.font)
.multilineTextAlignment(.leading)
}

static func suFooter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ struct ModalContent<VM: ModalVM, Header: View, Body: View, Footer: View>: View {
}

var body: some View {
VStack(
alignment: .leading,
spacing: self.model.contentSpacing
) {
VStack(spacing: self.model.contentSpacing) {
self.contentHeader()
.observeSize {
self.headerSize = $0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class UKBottomModalController: UKModalController<BottomModalVM> {
public override func layout() {
super.layout()

self.contentView.bottom(self.model.outerPaddings.bottom, safeArea: true)
self.contentViewBottomConstraint = self.contentView.bottom(self.model.outerPaddings.bottom, safeArea: true).bottom
}

// MARK: - UIViewController Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ public class UKCenterModalController: UKModalController<CenterModalVM> {
public override func layout() {
super.layout()

self.contentView.bottomAnchor.constraint(
self.contentViewBottomConstraint = self.contentView.bottomAnchor.constraint(
lessThanOrEqualTo: self.view.safeAreaLayoutGuide.bottomAnchor,
constant: -self.model.outerPaddings.bottom
).isActive = true
self.contentView.centerVertically()
)
self.contentViewBottomConstraint?.isActive = true

let verticalConstraint = self.contentView.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor)
verticalConstraint.isActive = true
verticalConstraint.priority = .defaultLow
}

// MARK: - UIViewController Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open class UKModalController<VM: ModalVM>: UIViewController {
public let model: VM

private var contentViewWidthConstraint: NSLayoutConstraint?
var contentViewBottomConstraint: NSLayoutConstraint?

// MARK: - Subviews

Expand Down Expand Up @@ -53,6 +54,12 @@ open class UKModalController<VM: ModalVM>: UIViewController {
fatalError("init(coder:) has not been implemented")
}

// MARK: Deinitialization

deinit {
NotificationCenter.default.removeObserver(self)
}

// MARK: - Lifecycle

open override func viewDidLoad() {
Expand All @@ -65,7 +72,7 @@ open class UKModalController<VM: ModalVM>: UIViewController {

// MARK: - Setup

/// Sets up the modal's subviews and gesture recognizers.
/// Sets up the modal's subviews, gesture recognizers and observers.
open func setup() {
self.view.addSubview(self.overlay)
self.view.addSubview(self.contentView)
Expand All @@ -89,13 +96,45 @@ open class UKModalController<VM: ModalVM>: UIViewController {
controller.handleTraitChanges()
}
}

NotificationCenter.default.addObserver(
self,
selector: #selector(self.handleKeyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)

NotificationCenter.default.addObserver(
self,
selector: #selector(self.handleKeyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
}

@objc func handleOverlayTap() {
guard self.model.closesOnOverlayTap else { return }
self.dismiss(animated: true)
}

@objc func handleKeyboardWillShow(notification: NSNotification) {
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? CGFloat ?? 0.25
UIView.animate(withDuration: duration) {
self.contentViewBottomConstraint?.constant = -keyboardHeight - self.model.contentPaddings.bottom + self.view.safeAreaInsets.bottom
Copy link
Collaborator Author

@mikhailChelbaev mikhailChelbaev Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keybardHeight contains bottom safe area inset + this is a safe area constraint, so bottom safe area inset value should be added back, otherwise it will be counted twice

self.view.layoutIfNeeded()
}
}
}

@objc func handleKeyboardWillHide(notification: NSNotification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? CGFloat ?? 0.25
UIView.animate(withDuration: duration) {
self.contentViewBottomConstraint?.constant = -self.model.contentPaddings.bottom
self.view.layoutIfNeeded()
}
}

// MARK: - Style

/// Applies styling to the modal's subviews.
Expand Down
Loading