I'm trying to allow scrolling in both axes when the content is larger than the screen.``` var body: some View { ScrollView([.horizontal, .vertical]) { Rectangle() .foregroundColor(Color.red) .frame(width: 500, height: 500)}```1. The scroll view centers the content instead of beginning at the top-leading.2. When scrolled to the trailing edge there's some whitespace (similarly the content bleeds past the leading edge.)How do I fix the above issues?
Post
Replies
Boosts
Views
Activity
I'm trying to update the root view of a UIHostingController but this happens without animation.lazy var hostingController = UIHostingController(rootView: ContentView(value: value))
func handleTap() {
let value = ...
withAnimation {
hostingController.rootView = ContentView(value: value)
}
}
I'm getting the following crash using my `UIView` subclass with `UIViewRepresentable`:(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x00007fff2c7cd373 SwiftUI`(extension in SwiftUI):__C.NSAttributedString.(MetricsCache in _4EAA3873E044FE8466A2EF8771E1058D).findMetrics(requestedSize: __C.CGSize) -> Swift.Optional<(extension in SwiftUI):__C.NSAttributedString.(Metrics in _4EAA3873E044FE8466A2EF8771E1058D)> + 1059
frame #1: 0x00007fff2c7ca659 SwiftUI`(extension in SwiftUI):__C.NSAttributedString.(MetricsCache in _4EAA3873E044FE8466A2EF8771E1058D).metrics(requestedSize: __C.CGSize) -> (extension in SwiftUI):__C.NSAttributedString.(Metrics in _4EAA3873E044FE8466A2EF8771E1058D) + 297
frame #2: 0x00007fff2c7ca506 SwiftUI`SwiftUI.StyledTextLayoutDelegate.sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 118
frame #3: 0x00007fff2c6c0b89 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeChildrenGenerallyWithConcreteMajorProposal(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 1273
frame #4: 0x00007fff2c6c2550 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren1(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 144
frame #5: 0x00007fff2c6be7d0 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> () + 272
frame #6: 0x00007fff2c6bdb3f SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 431
frame #7: 0x00007fff2c6bd986 SwiftUI`SwiftUI.StackLayout.(Storage in _68D684484B5AEF917B6B8353D57CF590).sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 70
frame #8: 0x00007fff2c6c0b89 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeChildrenGenerallyWithConcreteMajorProposal(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 1273
frame #9: 0x00007fff2c6c2550 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren1(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 144
frame #10: 0x00007fff2c6be7d0 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> () + 272
frame #11: 0x00007fff2c6bdca3 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).childGeometries(at: __C.CGSize) -> Swift.Array + 83
frame #12: 0x00007fff2c6d8dca SwiftUI`generic specialization <swiftui.layoutcontext.(childgeometriesquery in="" _b0b50b213a0802e1084335d0ad6cd036)<swiftui._formvstacklayout="">> of (extension in SwiftUI):SwiftUI.LayoutQuery< where A.Inputs == AttributeGraph.List2<__C.CGSize, SwiftUI.LayoutComputer>, A.Value == Swift.Array, A.Layout.PlacementContext == SwiftUI.LayoutContext>.update(context: inout AttributeGraph.AttributeContext) -> () + 122
frame #13: 0x00007fff2c5493aa SwiftUI`merged partial apply forwarder for protocol witness for static AttributeGraph.UntypedAttribute._update(_: Swift.UnsafeMutableRawPointer, graph: __C.AGGraphRef, attribute: __C.AGAttribute) -> () in conformance SwiftUI.(DynamicLayoutViewChildGeometry in _8B9C7F39264416187A895085215951BC) : AttributeGraph.UntypedAttribute in SwiftUI + 26
frame #14: 0x00007fff2fc2ed45 AttributeGraph`AG::Graph::UpdateStack::update() + 455
frame #15: 0x00007fff2fc2f253 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 373
frame #16: 0x00007fff2fc33d5b AttributeGraph`AG::Subgraph::update(unsigned int) + 729
frame #17: 0x00007fff2c51d690 SwiftUI`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> () + 224
frame #18: 0x00007fff2c51d446 SwiftUI`closure #1 (__C.AGGraphRef) -> () in SwiftUI.ViewGraph.flushTransactions() -> () + 262
frame #19: 0x00007fff2c51d125 SwiftUI`SwiftUI.ViewGraph.flushTransactions() -> () + 213
frame #20: 0x00007fff2c51d26f SwiftUI`closure #1 (SwiftUI.ViewGraph) -> () in closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> () + 15
frame #21: 0x00007fff2c51b539 SwiftUI`function signature specialization <arg[2] =="" [constant="" propagated="" function="" :="" closure="" #1="" (swiftui.viewgraph)="" -=""> () in closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> ()]> of reabstraction thunk helper from @callee_guaranteed (@guaranteed SwiftUI.ViewGraph) -> () to @escaping @callee_guaranteed (@guaranteed SwiftUI.ViewGraph) -> (@out ()) + 9
frame #22: 0x00007fff2c7f5ab7 SwiftUI`(extension in SwiftUI):SwiftUI.ViewRendererHost.updateViewGraph(body: (SwiftUI.ViewGraph) -> A1) -> A1 + 71
frame #23: 0x00007fff2c7f57b3 SwiftUI`protocol witness for SwiftUI.ViewGraphDelegate.updateViewGraph(body: (SwiftUI.ViewGraph) -> A1) -> A1 in conformance SwiftUI._UIHostingView : SwiftUI.ViewGraphDelegate in SwiftUI + 19
frame #24: 0x00007fff2c51d24a SwiftUI`closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> () + 122
frame #25: 0x00007fff2c53977c SwiftUI`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_guaranteed () -> (@out ()) + 12
frame #26: 0x00007fff2c3a1401 SwiftUI`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_guaranteed () -> (@out ()) + 17
frame #27: 0x00007fff2c3a1327 SwiftUI`static (extension in SwiftUI):__C.NSRunLoop.flushObservers() -> () + 119
frame #28: 0x00007fff2c3a12a9 SwiftUI`closure #1 (Swift.Optional<__C.CFRunLoopObserverRef>, __C.CFRunLoopActivity, Swift.Optional) -> () in static (extension in SwiftUI):__C.NSRunLoop.addObserver(() -> ()) -> () + 9
frame #29: 0x00007fff2c3a139b SwiftUI`@objc closure #1 (Swift.Optional<__C.CFRunLoopObserverRef>, __C.CFRunLoopActivity, Swift.Optional) -> () in static (extension in SwiftUI):__C.NSRunLoop.addObserver(() -> ()) -> () + 43
frame #30: 0x00007fff23d9d2a7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #31: 0x00007fff23d97d5e CoreFoundation`__CFRunLoopDoObservers + 430
frame #32: 0x00007fff23d982aa CoreFoundation`__CFRunLoopRun + 1226
frame #33: 0x00007fff23d97ac4 CoreFoundation`CFRunLoopRunSpecific + 404
frame #34: 0x00007fff38b2fc1a GraphicsServices`GSEventRunModal + 139
frame #35: 0x00007fff48bc7f80 UIKitCore`UIApplicationMain + 1605
* frame #36: 0x0000000101e9796b TestApp`main at AppDelegate.swift:9:7
frame #37: 0x00007fff519521fd libdyld.dylib`start + 1
(lldb)Seems like its a crash in `NSAttributedString.MetricsCache` - any idea what this is?I'm on Xcode 11.4
I'm trying to animate a layer's shadowPath property using UIViewPropertyAnimator but it looks like it's getting instantaneously set (instead of animating or being able to set the fraction complete on the animation.)
Is there an alternative way to do this?
view.layer.shadowPath = UIBezierPath(roundedRect: .init(origin: .zero, size: .init(width: 100, height: 100)), cornerRadius: 12).cgPath
let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut) {
view.layer.shadowPath = UIBezierPath(roundedRect: .init(origin: .zero, size: .init(width: 200, height: 200)), cornerRadius: 12).cgPath
}
animator.scrubsLinearly = false
animator.pauseAnimation()
animator.fractionComplete = 0
My app depends on some precompiled libraries that aren't built with Xcode 14 and I'm running into linker errors when building for device like the following:
Undefined symbol: _objc_msgSend$CGColor
Undefined symbol: _objc_msgSend$CGImage
Undefined symbol: _objc_msgSend$CGPointValue
Undefined symbol: _objc_msgSend$CGRectValue
Undefined symbol: _objc_msgSend$CPUArchForCPUType:subType:
Undefined symbol: _objc_msgSend$HTTPBody
Undefined symbol: _objc_msgSend$HTTPCookieRepresentationForDomain:
Undefined symbol: _objc_msgSend$HTTPHeaders
Undefined symbol: _objc_msgSend$HTTPMaximumConnectionsPerHost
Undefined symbol: _objc_msgSend$HTTPMethod
Can I turn off this optimization?
It's generally pretty easy to change the end point of an in-flight animation for a view that's on-screen by repeatedly mutating state:
withAnimation { state = ... }
var body: some View {
...
child
.opacity(state)
}
For a subview that is being removed, using the .transition modifier we have one chance to setup the endpoint like this:
disappearingChild
.transition(
.modifier(active: EndStateViewModifier(), identity: ...)
)
struct EndStateViewModifier: ViewModifier {
func body(content: Content) -> some View {
content
.opacity(0)
}
}
Is it possible to change the end point mid way through the animation?
Previously we could have some code in deinit that tears down local state:
class ViewController: UIViewController {
var displayLink: CADisplayLink?
deinit {
displayLink?.invalidate()
}
}
However this doesn't work in Swift 6 because we cannot access property 'displayLink' with a non-sendable type 'CADisplayLink?' from non-isolated context in deinit.
What's the right way to resolve this? Is the following a reasonable approach using Task to create an async context?
deinit {
Task {
await MainActor.run {
displayLink?.invalidate()
}
}
}
In the following snippet, it's obvious that matched geometry effect is overlaying both views and crossfading between them. Is there a way to evenly animate the corner radius change?
struct ContentView: View {
@State var toggled = false
@Namespace var namespace
var body: some View {
VStack(spacing: 16) {
Toggle("Toggle", isOn: $toggled.animation(.linear(duration: 5)))
if toggled {
RoundedRectangle(cornerRadius: 24, style: .continuous)
.fill(.red)
.matchedGeometryEffect(id: "box", in: namespace)
.frame(width: 200, height: 100)
Spacer()
} else {
Spacer()
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(.blue)
.matchedGeometryEffect(id: "box", in: namespace)
.frame(width: 100, height: 200)
}
}
.padding(24)
}
}
I'm trying to use a different version of view modifier when a view is inserted and when its being removed. However combining .asymmetric and .modifier transitions don't quite work as expected.
Here's a test that attempts to print out the combination of states the view transitions between:
struct TransitionPrinting: ViewModifier {
var isInsertion: Bool
var isActive: Bool
func body(content: Content) -> some View {
content
.onAppear {
print("isInsertion", isInsertion, "isActive", isActive)
}
}
}
struct ContentView: View {
@State var toggled = false
var body: some View {
VStack {
Toggle("Toggle", isOn: $toggled)
if toggled {
Text("Hello, world!")
.transition(
.asymmetric(
insertion: .modifier(
active: TransitionPrinting(isInsertion: true, isActive: true),
identity: TransitionPrinting(isInsertion: true, isActive: false)
),
removal: .modifier(
active: TransitionPrinting(isInsertion: false, isActive: true),
identity: TransitionPrinting(isInsertion: false, isActive: false)
)
)
)
}
Spacer()
}
.padding()
}
}
When the above is run, I only see the following in the simulator (with 2 of the states missing):
isInsertion true isActive true
isInsertion false isActive false
What's wrong here?