Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[S] Allow SwiftUI content view to size up until maximum width #241

Merged
merged 1 commit into from
Aug 26, 2022
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
34 changes: 31 additions & 3 deletions Demo/PopTip Demo/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import SwiftUI

@available(iOS 13.0, *)
struct SwiftUIView: View {
struct SwiftUIOneView: View {

var items = [Item(string: "First"),
Item(string: "Second"),
Expand Down Expand Up @@ -44,9 +44,37 @@ class Item: Identifiable {
}

@available(iOS 13.0, *)
struct SwiftUIView_Previews: PreviewProvider {
struct SwiftUIOneView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
SwiftUIOneView()
.previewLayout(.sizeThatFits)
}
}

@available(iOS 13.0, *)
struct SwiftUITwoView: View {

var body: some View {
HStack(alignment: .top, spacing: 8) {
VStack(alignment: .leading, spacing: 4) {
Text("This is a title")
Text("This is an AMPopTip which utilises a SwiftUI view as its content")
}
Spacer(minLength: 0)
Image(systemName: "bubble.left")
.resizable()
.scaledToFit()
.frame(width: 12, height: 12)
}
}
}

@available(iOS 13.0, *)
struct SwiftUITwoView_Previews: PreviewProvider {
static var previews: some View {
SwiftUITwoView()
.previewLayout(.sizeThatFits)
}
}

#endif
9 changes: 8 additions & 1 deletion Demo/PopTip Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import SwiftUI
import AMPopTip

class ViewController: UIViewController {
Expand Down Expand Up @@ -82,6 +83,7 @@ class ViewController: UIViewController {
autolayoutView?.frame.size = CGSize(width: 180, height: 100)
}

var swiftUIView = false
var showSwiftUIView = false
var showMaskAndCutout = false

Expand Down Expand Up @@ -140,7 +142,12 @@ class ViewController: UIViewController {
}
else if #available(iOS 13.0.0, *) {
#if canImport(SwiftUI) && canImport(Combine)
popTip.show(rootView: SwiftUIView(), direction: .down, in: view, from: sender.frame, parent: self)
if swiftUIView {
popTip.show(rootView: SwiftUIOneView(), direction: .down, in: view, from: sender.frame, parent: self)
} else {
popTip.show(rootView: SwiftUITwoView(), direction: .down, in: view, from: sender.frame, parent: self)
}
self.swiftUIView.toggle()
#endif
}
popTip.entranceAnimationHandler = { [weak self] completion in
Expand Down
4 changes: 3 additions & 1 deletion Source/PopTip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,9 @@ open class PopTip: UIView {
containerView = view
let controller = UIHostingController(rootView: rootView)
controller.view.backgroundColor = .clear
controller.view.frame.size = controller.view.intrinsicContentSize
let maxContentWidth = UIScreen.main.bounds.width - (self.edgeMargin * 2) - self.edgeInsets.horizontal - (self.padding * 2)
let sizeThatFits = controller.view.sizeThatFits(CGSize(width: maxContentWidth, height: CGFloat.greatestFiniteMagnitude))
controller.view.frame.size = CGSize(width: min(sizeThatFits.width, maxContentWidth), height: sizeThatFits.height)
maxWidth = controller.view.frame.size.width
self.customView?.removeFromSuperview()
self.customView = controller.view
Expand Down