Skip to content

Commit

Permalink
Fix content width bug (#64)
Browse files Browse the repository at this point in the history
* Fix content width bug

When the title and subtitle are too long, the notification banner isn't centered anymore, but gets off-screen.

* Update Sources/UINotificationPresentationContext.swift

Co-authored-by: Amir Khorsandi <[email protected]>

* Remove whitespace

* Clea up whitespace

* Fix test while also keeping the width fix

* Fix indentation

* Use leading and trailing instead of left and right

Co-authored-by: Amir Khorsandi <[email protected]>
  • Loading branch information
kevinrenskers and amirdew authored Mar 28, 2022
1 parent de6884c commit 68aa9d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
43 changes: 25 additions & 18 deletions Sources/UINotificationPresentationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,71 @@ import UIKit

/// Provides information about an in-progress notification presentation.
public final class UINotificationPresentationContext {

/// The window in which the `UINotificationView` will be presented.
public let containerWindow: UIWindow

/// The level the container window should be on when presenting the notification
private let windowLevel: UIWindow.Level

/// The `UINotificationView` containing the visual representation of the `UINotification`.
public let notificationView: UINotificationView

/// The notification request currenly being handled.
private let request: UINotificationRequest

/// Public getter for the current notification which is handled.
public var notification: UINotification {
return request.notification
}

internal init(request: UINotificationRequest, containerWindow: UIWindow, windowLevel: UIWindow.Level, notificationView: UINotificationView) {
self.request = request
self.containerWindow = containerWindow
self.notificationView = notificationView
self.windowLevel = windowLevel

prepareContainerWindow()
prepareNotificationView()
}

/// Completes the presentation. Resets the container window and updates the state of the `UINotificationRequest`.
public func completePresentation() {
resetContainerWindow()
notificationView.removeFromSuperview()
request.finish()

// This releases all objects.
// We can't define the presenter weak inside the notificationView, because it's needed for dismissing after a pan gesture.
notificationView.presenter = nil
}

private func prepareContainerWindow() {
containerWindow.windowLevel = windowLevel
#if !TEST
containerWindow.isHidden = false
#endif
}

private func prepareNotificationView() {
/// Create a container view controller to let it handle orientation changes.
let containerViewController = UIViewController(nibName: nil, bundle: nil)
containerViewController.view.addSubview(notificationView)
containerWindow.rootViewController = containerViewController

/// Set the top constraint.
var notificationViewTopConstraint = notificationView.topAnchor.constraint(equalTo: containerViewController.view.topAnchor, constant: -notification.style.height.value)

/// For iPhone X we need to use the safe area layout guide, which is only available in iOS 11 and up.
if #available(iOS 11.0, *) {
notificationViewTopConstraint = notificationView.topAnchor.constraint(equalTo: containerViewController.view.safeAreaLayoutGuide.topAnchor, constant: -notification.style.height.value)
}

notificationView.topConstraint = notificationViewTopConstraint

var constraints = [
notificationView.leftAnchor.constraint(equalTo: containerViewController.view.leftAnchor).usingPriority(.defaultHigh),
notificationView.rightAnchor.constraint(equalTo: containerViewController.view.rightAnchor).usingPriority(.defaultHigh),
notificationView.leadingAnchor.constraint(equalTo: containerViewController.view.leadingAnchor).usingPriority(.almostRequired),
notificationView.trailingAnchor.constraint(equalTo: containerViewController.view.trailingAnchor).usingPriority(.almostRequired),
notificationView.heightAnchor.constraint(equalToConstant: notification.style.height.value),
notificationViewTopConstraint
]
Expand All @@ -89,11 +89,18 @@ public final class UINotificationPresentationContext {
NSLayoutConstraint.activate(constraints)
containerViewController.view.layoutIfNeeded()
}

private func resetContainerWindow() {
/// Move the window behind the key application window.
containerWindow.windowLevel = UIWindow.Level.normal - 1
containerWindow.rootViewController = nil
containerWindow.isHidden = true
}
}

private extension UILayoutPriority {
/// Creates a priority which is almost required, but not 100%.
static var almostRequired: UILayoutPriority {
UILayoutPriority(rawValue: 999)
}
}
4 changes: 2 additions & 2 deletions Sources/UINotificationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ open class UINotificationView: UIView {
imageViewHeightConstraint = imageView.heightAnchor.constraint(equalToConstant: 31)

let constraints = [
containerStackView.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor, constant: 18),
containerStackView.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor, constant: -18),
containerStackView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor, constant: 18),
containerStackView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor, constant: -18),
containerStackView.topAnchor.constraint(equalTo: topAnchor, constant: layoutMargins.top),
containerStackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor, constant: 0),

Expand Down

0 comments on commit 68aa9d3

Please sign in to comment.