Skip to content

Commit

Permalink
Merge pull request #241 from Grimlock257/s/fix-swift-ui-width
Browse files Browse the repository at this point in the history
[S] Allow SwiftUI content view to size up until maximum width
  • Loading branch information
andreamazz authored Aug 26, 2022
2 parents 85e32bc + d871b35 commit cf9e151
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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

0 comments on commit cf9e151

Please sign in to comment.