On macOS 15.4 beta 2 (24E5222f), the back button is not visible when navigating from a view with no toolbar to a view with a toolbar button. The back button is visible on macOS 15.3.1 and earlier versions.
Also, the toolbar button shown when run on macOS 15.4 beta 2 is truncated.
This is how it looks on macOS 15.4 beta 2:
And this is how it looks on macOS 15.3.1 and earlier:
Feedback ID: FB16743834
SwiftUI
RSS for tagProvide views, controls, and layout structures for declaring your app's user interface using SwiftUI.
Post
Replies
Boosts
Views
Activity
So I am looking to use a custom NSWindow application (so I can implement some enhanced resizing/dragging behavior which is only possible overriding NSWindow).
The problem is my whole application is currently SwiftUI-based (see the project here: https://github.com/msdrigg/Roam/blob/50a2a641aa5f2fccb4382e14dbb410c1679d8b0c/Roam/RoamApp.swift).
I know there is a way to make this work by dropping my @main SwiftUI app and replacing it with a SwiftUI root view hosted in a standard AppKit root app, but that feels like I'm going backwards.
Is there another way to get access (and override) the root NSWindow for a SwiftUI app?
I'm attempting to write a macOS version of https://stackoverflow.com/a/74935849/2178159.
From my understanding, I should be able to set the menu property of an NSResponder and it will automatically show on right click.
I've tried a couple things:
A: set menu on an NSHostingController's view - when I do this and right or ctrl click, nothing happens.
B: set menu on NSHostingController directly - when I do this I get a crash Abstract method -[NSResponder setMenu:] called from class _TtGC7SwiftUI19NSHostingControllerGVS_21_ViewModifier_...__. Subclasses must override
C: manually call NSMenu.popup in a custom subclasses of NSHostingController or NSView's rightMouseDown method - nothing happens.
extension View {
func contextMenu(menu: NSMenu) -> some View {
modifier(ContextMenuViewModifier(menu: menu))
}
}
struct ContextMenuViewModifier: ViewModifier {
let menu: NSMenu
func body(content: Content) -> some View {
Interaction_UI(
view: { content },
menu: menu
)
.fixedSize()
}
}
private struct Interaction_UI<Content: View>: NSViewRepresentable {
typealias NSViewType = NSView
@ViewBuilder var view: Content
let menu: NSMenu
func makeNSView(context: Context) -> NSView {
let v = NSHostingController(rootView: view)
// option A - no effect
v.view.menu = menu
// option B - crash
v.menu = menu
return v.view
}
func updateNSView(_ nsView: NSViewType, context: Context) {
// part of option A
nsView.menu = menu
}
}
Hi,
I'll start by saying that I'm a new developer for apps for Apple devices, especially for Apple Watch, so please have mercy
I'm trying to create an app for Apple Watch (WatchOs 10+) and I have a problem, my interface is made up of 3 buttons, one at the bottom of the screen and two smaller ones positioned on the top of the screen, each in their respective corners (one at the top right and the other at the top left).
By positioning these buttons on the top of the screen, the top right button is covered by the default Apple time, and I would like to move it to the center, thus creating two side buttons and the time in the center.
I'm also asking if there's a way to remove it since it's not useful to me, but reading some forums it seems that without it I don't pass the app verification, so I'm waiting for your advice.
The only app I've seen in circulation on the app store that has the centered clock is petey.
I'm trying to combine a RotateGesture and a MagnifyGesture within a single SwiftUI view using SimultaneousGesture. My goal is to allow users to rotate and zoom an image (potentially at the same time). However, I’m running into a problem:
If only one gesture (say, the magnification) starts and finishes without triggering the other (rotation), it seems that the rotation gesture is considered "failed." After that, no further .onChanged or .onEnded callbacks fire for either gesture until the user lifts their fingers and starts over.
Here’s a simplified version of my code:
struct ImageDragView: View {
@State private var scale: CGFloat = 1.0
@State private var lastScale: CGFloat = 1.0
@State private var angle: Angle = .zero
@State private var lastAngle: Angle = .zero
var body: some View {
Image("Stickers3")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 100)
.rotationEffect(angle, anchor: .center)
.scaleEffect(scale)
.gesture(combinedGesture)
}
var combinedGesture: some Gesture {
SimultaneousGesture(
RotateGesture(minimumAngleDelta: .degrees(8)),
MagnifyGesture()
)
.onChanged { combinedValue in
if let magnification = combinedValue.second?.magnification {
let minScale = 0.2
let maxScale = 5.0
let newScale = magnification * lastScale
scale = max(min(newScale, maxScale), minScale)
}
if let rotation = combinedValue.first?.rotation {
angle = rotation + lastAngle
}
}
.onEnded { _ in
lastScale = scale
lastAngle = angle
}
}
}
If I pinch and rotate together (or just rotate), both gestures work as expected.
But if I only pinch (or, sometimes, if the rotation amount doesn’t meet minimumAngleDelta), subsequent gestures don’t trigger the .onChanged or .onEnded callbacks anymore, as if the entire gesture sequence is canceled.
I found that setting minimumAngleDelta: .degrees(0) helps because then rotation almost never fails. But I’d like to understand why this happens and whether there’s a recommended way to handle the situation where one gesture might be recognized but not the other, without losing the gesture recognition session entirely.
Is there a known workaround or best practice for combining a pinch and rotate gesture where either one might occur independently, but we still want both gestures to remain active?
Any insights would be much appreciated!
In UIKit, certain events like a button tap can be simulated using:
button.sendActions(for: .touchUpInside)
This allows us to trigger the button’s action programmatically.
However, in SwiftUI, there is no direct equivalent of sendActions(for:) for views like Button. What is the recommended approach to programmatically simulate a SwiftUI button tap and trigger its action?
Is there an alternative mechanism to achieve this(and for other events under UIControl.event) , especially in scenarios where we want to test interactions or trigger actions without direct user input?
Using the new floating tab bar with ios18 on ipad with an RTL language, when the tabbar is correctly flipped and the right buttons become left buttons, the buttons are pushed off the screen on initial load but do come back after some navigating / reloading, have recreated in a fresh project.
I'm using GoogleMaps in my project.
Legacy preview works well but new preview (Xcode 16.3.1 beta) produces error. It doesn't seem to find Googlemaps.a.
== PREVIEW UPDATE ERROR:
FailedToLaunchAppError: Failed to launch ***
==================================
| [Remote] JITError
|
| ==================================
|
| | [Remote] CouldNotLoadInputStaticArchiveFile: Could not load static archive during preview: /Users/***/Library/Developer/Xcode/DerivedData/BOA-eiluspltxasszsfkpqrnnsxsjhth/Build/Products/Debug_BOA_Inhouse-iphonesimulator/GoogleMaps.a
| |
| | path: /Users/***/Library/Developer/Xcode/DerivedData/BOA-eiluspltxasszsfkpqrnnsxsjhth/Build/Products/Debug_BOA_Inhouse-iphonesimulator/GoogleMaps.a
| |
| | ==================================
| |
| | | [Remote] XOJITError
| | |
| | | XOJITError: arm64 slice of /Users/***/Library/Developer/Xcode/DerivedData/BOA-eiluspltxasszsfkpqrnnsxsjhth/Build/Products/Debug_BOA_Inhouse-iphonesimulator/GoogleMaps.a does not contain an archive
I have view showing a list of contacts. When the user taps one, I want to raise a sheet that shows the contact's phone numbers and E-mail addresses and lets the user pick one.
When the user taps a list entry, I store the associated Contact object into a @State variable called selectedContact. Then I set the boolean that's bound to the sheet modifier's isPresented flag. That modifier:
.sheet(isPresented: $showContactMethodSheet, content: { ContactMethodView(withContact: selectedContact!) })
But the app crashes, because despite selectedContact having been set. It looks like the sheet was pre-built with a nil selected contact upon view load. Why, and what is the expected approach here?
We have a widget bundle with multiple widgets.
I'm adding a widget that is interactive (iOS 17 and higher).
Our widget code is in a static library that gets linked into the widget extension target in our main app Xcode project.
I have SwiftUI buttons constructed with the intent constructor in our UI
See https://developer.apple.com/documentation/swiftui/button/init(intent:label:)
When I press the button the timeline refreshes (conforming to TimelineProvider) but the perform method doesn't seem to be called.
I've seen multiple pieces of advice and none of them seem to work.
I've tried on a physical device and a simulator.
I've tried adding an AppIntentsPackage.
I've tried including the AppIntent code in the app and the widget.
I've tried setting the openAppWhenRun to true and false and not setting it at all.
I've tried simplifying the intent to just printing out a line to the console and returning a result.
At this point I have no idea how to debug this and I don't know what else to try.
I appreciate any helpful advice at this point.
I have created an AppIntent and added it to shortcuts to be able to read by Siri. When I say the phrase, the Siri intent dialog appears just fine. I have added a custom SwiftUI View inside Siri dialog box with 2 buttons with intents. The callback or handling of those buttons is not working when initiated via Siri. It works fine when I initiate it in shortcuts. I tried using the UIButton without the intent action as well but it did not work. Here is the code.
static let title: LocalizedStringResource = "My Custom Intent"
static var openAppWhenRun: Bool = false
@MainActor
func perform() async throws -> some ShowsSnippetView & ProvidesDialog {
return .result(dialog: "Here are the details of your order"), content: {
OrderDetailsView()
}
}
struct OrderDetailsView {
var body: some View {
HStack {
if #available(iOS 17.0, *) {
Button(intent: ModifyOrderIntent(), label : {
Text("Modify Order")
})
Button(intent: CancelOrderIntent(), label : {
Text("Cancel Order")
})
}
}
}
}
struct ModifyOrderIntent: AppIntent {
static let title: LocalizedStringResource = "Modify Order"
static var openAppWhenRun: Bool = true
@MainActor
func perform() async throws -> some OpensIntent {
// performs the deeplinking to app to a certain page to modify the order
}
}
struct CancelOrderIntent: AppIntent {
static let title: LocalizedStringResource = "Cancel Order"
static var openAppWhenRun: Bool = true
@MainActor
func perform() async throws -> some OpensIntent {
// performs the deeplinking to app to a certain page to cancel the order
}
}
Button(action: {
if let url = URL(string: "myap://open-order") {
UIApplication.shared.open(url)
}
}
Platform Specs:
Xcode 16.2
Swift 6.0.3
iOS 18.2 + iOS Simulator 18.3.1
Issue:
Refer to the following code:
struct CustomView: View {
@Binding var prop: CustomStruct
init(prop p: Binding<CustomStruct>) {
_prop = p
}
init(isPreview: Bool) {
let p = CustomStruct()
_prop = .constant(p)
}
var body: some View {
VStack {
Text("hi")
}
}
}
#Preview {
CustomView(isPreview: true)
.preferredColorScheme(.dark)
}
The first constructor is for normal app functionality (and previews/functions correctly when used with the rest of the app in the ContentView preview tab). The second constructor is for previewing only CustomView in its own preview tab. This constructor does not work when previewing in the same file, as shown above. It triggers an ambiguous crash, stating that the diagnostic log (which obviously provides no clear information) should be checked.
I have isolated the issue to be in the Binding reassignment in the second constructor. Replacing CustomStruct with anything but another struct, like an enum or primitive, fixes the issue.
Note: This bug only occurs when previewing (either through the #Preview macro or PreviewProvider struct).
In the example code below:
OuterView has a @State var number: Int? = 0
InnerView expects a binding of type Int.
OuterView uses Binding.init(_: Binding<Value?>) to convert from Binding<Int?> to Binding<Int>?.
If OuterView sets number to nil, there is a runtime crash:
BindingOperations.ForceUnwrapping.get(base:) + 160
InnerView is being evaluated (executing body?) when number is nil despite the fact that it shouldn't exist in that configuration. Is this a bug or expected behavior?
struct OuterView: View {
@State var number: Int? = 1
var body: some View {
if let binding = Binding($number) {
InnerView(number: binding)
}
Button("Doesn't Crash") { number = 0 }
Button("Crashes") { number = nil }
}
}
struct InnerView: View {
@Binding var number: Int
var body: some View {
Text(number.formatted())
}
}
There is a workaround that involves adding an extension to Optional<Int>:
extension Optional<Int> {
var nonOptional: Int {
get { self ?? 0 }
set { self = newValue }
}
}
And using that to ensure that the binding has a some value even when number is nil.
if number != nil {
InnerView(number: $number.nonOptional)
}
This works, but I don't understand why it's necessary. Any insight would be greatly appreciated!
Hi,
Previously, we would conform model objects to the ObservableObject protocol and use the @StateObject property wrapper when storing them to an owned binding in a View.
Now, if I understand correctly, it is recommended that we use the new @Observable macro/protocol in place of ObservableObject and use the @State property wrapper rather than @StateObject. This is my understanding from documentation articles such as Migrating from the Observable Object protocol to the Observable macro.
However, the StateObject property wrapper has an initialiser which takes an autoclosure parameter:
extension StateObject {
public init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType)
}
This is an extremely important initialiser for state objects that are expensive to allocate. As far as I can tell, the @State property wrapper lacks an equivalent initialiser.
What is the recommended migration strategy for objects which made use of this on StateObject?
Thanks
Hello! We can animate Text color via foregroundStyle very nicely in SwiftUI like so:
Text("Some text here")
.foregroundStyle(boolValue ? Color.green : Color.blue)
withAnimation {
boolValue.toggle()
}
However, if the foregroundStyle is a gradient, the color of the Text view changes immediately without animation.
The code below works to animate a gradient foregroundStyle on an SF Symbol, but it does not work when applied to a Text view. Is it possible to animate a Text view foregroundStyle between gradient values?
Image(systemName: "pencil.circle.fill")
.foregroundStyle(boolValue ? .linearGradient(colors: [.red, .orange], startPoint: .top, endPoint: .bottom) : .linearGradient(colors: [.green, .blue], startPoint: .top, endPoint: .bottom))
Thanks for your help!
Hi, when I run NavigationSplitView as shown below, the sidebar opens and closes smoothly.
struct TestingView: View {
var body: some View {
NavigationSplitView {
Text("Sidebar")
} detail: {
Text("Detail")
}
}
}
However, when I add a minimum width, the sidebar animation starts to lag. I tried various wapping solutions, but I couldn’t get rid of the stutter.
struct TestingView: View {
var body: some View {
NavigationSplitView {
Text("Sidebar")
} detail: {
Text("Detail")
}
.frame(minWidth: 800)
}
}
As a solution, I had to disable the animation completely:
.animation(nil, value: columnVisibility)
Is there a way to keep the animation without the stuttering?
I have a USDZ 3d model contains a running man animation and I manage to run the model well using this code:
import SwiftUI
import RealityKit
struct ContentView: View {
@State var animationResource: AnimationResource?
@State var animationDefinition: AnimationDefinition?
@State var manPlayer = Entity()
@State var speed:Float = 0.5
var body: some View {
VStack {
RealityView { content in
if let item = try? await Entity(named: "run.usdz") {
manPlayer = item
content.add(manPlayer)
}
}
HStack {
Button(action: playThis) {
Text("Play")
}
Button(action: increaseSpeed) {
Text("increase Speed (+) ")
}
Button(action: decreaseSpeed) {
Text("decrease Speed (-) ")
}
}
}
}
func playThis() {
animationResource = manPlayer.availableAnimations[0]
animationDefinition = animationResource?.definition
animationDefinition?.repeatMode = .repeat
animationDefinition?.speed = speed
// i could not add the definition to the animation resource back again
// so i generated a new one
let animRes = try! AnimationResource.generate(with: animationDefinition!)
manPlayer.playAnimation(animRes)
}
func increaseSpeed() {
speed += 0.1
animationDefinition?.speed = speed
// Now i wonder is this the best way to increase speed
// isn't it as if im adding more load to the memory
// by adding another players
let animRes = try! AnimationResource.generate(with: animationDefinition!)
manPlayer.playAnimation(animRes)
}
func decreaseSpeed() {
speed -= 0.1
animationDefinition?.speed = speed
// Now i wonder is this the best way to increase speed
// isn't it as if im adding more load to the memory
// by adding another players
let animRes = try! AnimationResource.generate(with: animationDefinition!)
manPlayer.playAnimation(animRes)
}
}
how to control speed of this animation while it is running without have to regenerate a resource and replay the animation over and over with every speed change knowing that every time the animation replayed it started from the frame zero meaning its not completing its cycle before its replayed but cut in the middle and start over again from start which I do not prefer to be happen.
The USDZ file is here if you wish to try https://www.worldhotelity.com/stack/run.usdz
I found the Table with Toggle will have performance issue when the data is large.
I can reproduce it in Apple demo:
https://developer.apple.com/documentation/swiftui/building_a_great_mac_app_with_swiftui
Replace with a large mock data, for example
database.json
Try to scroll the table, it's not smooth.
I found if I delete the Toggle, the performance be good.
TableColumn("Favorite", value: \.favorite, comparator: BoolComparator()) { plant in
Toggle("Favorite", isOn: $garden[plant.id].favorite)
.labelsHidden()
}
Is this bug in SwiftUI? Any workaround?
My Mac is Intel, not sure it can repro on Apple Silicon
The behavior of the Button in ScrollView differs depending on how the View is displayed modally.
When the View is displayed as a .fullScreenCover, if the button is touched and scrolled without releasing the finger, the touch event is canceled and the action of the Button is not called.
On the other hand, if the View is displayed as a .sheet, the touch event is not canceled even if the view is scrolled without lifting the finger, and the action is called when the finger is released.
In order to prevent accidental interaction, I feel that the behavior of .fullScreenCover is better, as it cancels the event immediately when scrolling. Can I change the behavior of .sheet?
Demo movie is here:
https://x.com/kenmaz/status/1896498312737611891
Sample code
import SwiftUI
@main
struct SampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var showSheet = false
@State private var showFullScreen = false
var body: some View {
VStack(spacing: 16) {
Button("Sheet") {
showSheet.toggle()
}
Button("Full screen") {
showFullScreen.toggle()
}
}
.sheet(isPresented: $showSheet) {
SecondView()
}
.fullScreenCover(isPresented: $showFullScreen) {
SecondView()
}
.font(.title)
}
}
struct SecondView: View {
@Environment(\.dismiss) var dismiss
var body: some View {
ScrollView {
Button("Dismiss") {
dismiss()
}
.buttonStyle(MyButtonStyle())
.padding(.top, 128)
.font(.title)
}
}
}
private struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration
.label
.foregroundStyle(.red)
.background(configuration.isPressed ? .gray : .clear)
}
}
I have a view with some buttons, and add 2 gestures using simultaneously. My app works well when built with XCode less than 16, or run on iOS less than 18.0.
Example code is below:
VStack(spacing: 0) {
Button {
print("button tapped")
} label: {
Rectangle()
.foregroundColor(.red)
}
.frame(height: 100)
}
.gesture(
DragGesture(minimumDistance: 0)
.onEnded { value in
print("single tap")
}
.simultaneously(with:
TapGesture(count: 2).onEnded {
print("double tap")
}
)
)
.frame(width: 200, height: 200)
.border(Color.purple)
I expect the action on Button should be recognized and print out button tapped, but only single tap and double tap are recognized