Skip to content

Commit

Permalink
Couple of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wtmoose committed Nov 1, 2023
1 parent c2bcb35 commit 62e12e1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log
All notable changes to this project will be documented in this file.

## 9.0.9

### Fixes

* Fix hit testing on SwiftUI views to allow touches around the view's margins to pass through to the underlying view.
* Update `KeyboardTrackingView` to continue tracking the keyboard even when not installed in the view hierarchy.

## 9.0.8

### Changes
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ struct DemoMessageView: View {
}
.multilineTextAlignment(.leading)
.padding(30)
// This makes the message width greedy
.frame(maxWidth: .infinity)
.background(.gray)
// This makes a tab-style view where the bottom corners are rounded and the view's background
// extends to the top edge.
// This makes a tab-style view where the bottom corners are rounded and
// the view's background extends to the top edge.
.mask(
UnevenRoundedRectangle(
cornerRadii: .init(bottomLeading: 15, bottomTrailing: 15)
)
UnevenRoundedRectangle(bottomLeadingRadius: 15, bottomTrailingRadius: 15)
// This causes the background to extend into the safe area to the screen edge.
.edgesIgnoringSafeArea(.top)
)
}
Expand Down
2 changes: 1 addition & 1 deletion SwiftMessages.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'SwiftMessages'
spec.version = '9.0.8'
spec.version = '9.0.9'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/SwiftKickMobile/SwiftMessages'
spec.authors = { 'Timothy Moose' => 'tim@swiftkickmobile.com' }
Expand Down
17 changes: 14 additions & 3 deletions SwiftMessages/KeyboardTrackingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ open class KeyboardTrackingView: UIView {

private var isAutomaticallyPaused = false
private var heightConstraint: NSLayoutConstraint!
private var lastObservedKeyboardRect: CGRect?

private func postInit() {
translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -109,15 +110,19 @@ open class KeyboardTrackingView: UIView {
isAutomaticallyPaused = false
}

open override func layoutSubviews() {
super.layoutSubviews()
heightConstraint.constant = calculateHeightConstant()
}

private func show(change: Change, _ notification: Notification) {
guard !(isPaused || isAutomaticallyPaused),
let userInfo = (notification as NSNotification).userInfo,
let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
willChange(change: change, userInfo: userInfo)
delegate?.keyboardTrackingViewWillChange(change: change, userInfo: userInfo)
let keyboardRect = value.cgRectValue
let thisRect = convert(bounds, to: nil)
let newHeight = max(0, thisRect.maxY - keyboardRect.minY) + topMargin
lastObservedKeyboardRect = value.cgRectValue
let newHeight = calculateHeightConstant()
guard heightConstraint.constant != newHeight else { return }
animateKeyboardChange(change: change, height: newHeight, userInfo: userInfo)
}
Expand All @@ -140,4 +145,10 @@ open class KeyboardTrackingView: UIView {
CATransaction.commit()
}
}

private func calculateHeightConstant() -> CGFloat {
guard let keyboardRect = lastObservedKeyboardRect else { return 0 }
let thisRect = convert(bounds, to: nil)
return max(0, thisRect.maxY - keyboardRect.minY) + topMargin
}
}
8 changes: 8 additions & 0 deletions SwiftMessages/MessageHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public class MessageHostingView<Content>: BaseView, Identifiable where Content:
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
// The rendered SwiftUI view isn't a direct child of this hosting view. SwiftUI
// inserts another intermediate view that should also ignore touches.
if view == self || view?.superview == self { return nil }
return view
}
}
3 changes: 2 additions & 1 deletion SwiftUIDemo/SwiftUIDemo/DemoMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import SwiftMessages
struct DemoMessage: Identifiable {
let title: String
let body: String
let style: DemoMessageView.Style

var id: String { title + body }
}

extension DemoMessage: MessageViewConvertible {
func asMessageView() -> DemoMessageView {
DemoMessageView(message: self)
DemoMessageView(message: self, style: style)
}
}
46 changes: 38 additions & 8 deletions SwiftUIDemo/SwiftUIDemo/DemoMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@

import SwiftUI

// A card-style message view
struct DemoMessageView: View {

// MARK: - API

enum Style {
case standard
case card
case tab
}

let message: DemoMessage
let style: Style


// MARK: - Variables

Expand All @@ -20,21 +29,42 @@ struct DemoMessageView: View {
// MARK: - Body

var body: some View {
switch style {
case .standard:
content()
// Mask the content and extend background into the safe area.
.mask {
Rectangle()
.edgesIgnoringSafeArea(.top)
}
case .card:
content()
// Mask the content with a rounded rectangle
.mask {
RoundedRectangle(cornerRadius: 15)
}
// External padding around the card
.padding(10)
case .tab:
content()
// Mask the content with rounded bottom edge and extend background into the safe area.
.mask {
UnevenRoundedRectangle(bottomLeadingRadius: 15, bottomTrailingRadius: 15)
.edgesIgnoringSafeArea(.top)
}
}
}

@ViewBuilder private func content() -> some View {
VStack(alignment: .leading) {
Text(message.title).font(.system(size: 20, weight: .bold))
Text(message.body)
}
.multilineTextAlignment(.leading)
// Internal padding of the card
.padding(30)
// Greedy width
.frame(maxWidth: .infinity)
.background(.demoMessageBackground)
// This makes a tab-style view where the bottom corners are rounded and the view's background
// extends to the top edge.
.mask(
UnevenRoundedRectangle(
cornerRadii: .init(bottomLeading: 15, bottomTrailing: 15)
)
.edgesIgnoringSafeArea(.top)
)
}
}
26 changes: 23 additions & 3 deletions SwiftUIDemo/SwiftUIDemo/DemoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,30 @@ struct DemoView: View {
@State var message: DemoMessage?

var body: some View {
Button("Show message") {
message = DemoMessage(title: "Demo", body: "This is a sample SwiftUI message! This content should be long enough to wrap.")
VStack {
Button("Show standard message") {
message = DemoMessage(
title: "Demo",
body: "This is a sample SwiftUI card-style message! This content should be long enough to wrap.",
style: .standard
)
}
Button("Show card message") {
message = DemoMessage(
title: "Demo",
body: "This is a sample SwiftUI card-style message! This content should be long enough to wrap.",
style: .card
)
}
Button("Show tab message") {
message = DemoMessage(
title: "Demo",
body: "This is a sample SwiftUI card-style message! This content should be long enough to wrap.",
style: .tab
)
}
}
.buttonBorderShape(.roundedRectangle(radius: 15))
.buttonStyle(.bordered)
.swiftMessage(message: $message)
}
}
Expand Down

0 comments on commit 62e12e1

Please sign in to comment.