Post

Replies

Boosts

Views

Activity

Reply to How can I set the height of a UITextView to exactly match its truncated content?
There are a few ways to solve this. One thing you can try is by fixing the width with a constraint, then set the truncated string you want first, call sizeToFit() on the textView and let it lay out, then set the text to a longer string. Here's a quick playground: import UIKit import PlaygroundSupport let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 150)) /* fix the width so the height can be adjusted */ textView.widthAnchor.constraint(equalToConstant: 200).isActive = true let longMessage = "This is a tale of a text view that is probably going to be too small to fit this large text because it is very wordy. Hopefully this will demonstrate the issue." textView.text = String(longMessage.prefix(100) + "...") textView.sizeToFit() textView.text = longMessage PlaygroundPage.current.liveView = textView
Jun ’20
Reply to Moving Binding for PageViewController using UIViewControllerRepresentable breaks pagination/animation
With the help of an engineer in the SwiftUI Lab, I was able to figure out what was going on with this sample. First, the UIHostingController array is problematic, when contained in a SwiftUI View that can be re-created at any time. See the Coordinator methods that all check controllers.firstIndex(of:...). When the views are re-created, none of the equality checks for the UIHostingController instances will evaluate to true anymore, hence breaking our paging methods. The solution I came up with was to push the generic <Page> all the way to the PageViewController, and pass those to the Coordinator. Then the Coordinator creates the actual UIViewController pages. Since the Coordinator lives on, these instances are the same across re-renders of the containing SwiftUI views. I filed a radar for the tutorial to be improved to not recommend this pattern, as it was really difficult to track down. FB7775209.
Jun ’20