Provide views, controls, and layout structures for declaring your app's user interface using SwiftUI.

SwiftUI Documentation

Posts under SwiftUI tag

2,345 Posts
Sort by:
Post not yet marked as solved
3 Replies
4.0k Views
Hi,When testing my SwiftUI code, I sometimes get the new Xcode preview system having this error:Error Domain=IXErrorDomain Code=29 "A coordinated install for xxxx is already pending." UserInfo={NSLocalizedDescription=A coordinated install for xxxx is already pending., SourceFileLine=143, FunctionName=+[IXAppInstallCoordinator(IXSimpleInstaller) _beginInstallForURL:consumeSource:options:completion:]_block_invoke, NSLocalizedFailureReason=Unhandled reason for code: 29 in domain IXErrorDomain}The workaround I have found is to reboot my Mac (I will edit this post if I find a less radical workaround).Note: Version 11.0 beta 2 (11M337n)
Posted
by
Post not yet marked as solved
2 Replies
5.8k Views
I know it's possible to set the line limit to nil, enabling a text view to grow, but is it possible to disable truncation completely? In some odd text wrapping situations, it seems the views still prefer to truncate rather than grow an additional line, especially in cases where only a partial word would wrap.
Posted
by
Post not yet marked as solved
17 Replies
12k Views
Hi,Are there any plans for integrating Metal/MetalKit with SwiftUI?Is that correct that current recommended way of the integration is to use UIViewControllerRepresentable like in the tutorial?Thank you!
Posted
by
Post not yet marked as solved
6 Replies
16k Views
What is the SwiftUI equivalent to returnKeyType? How do we implement it?
Posted
by
Post marked as solved
12 Replies
12k Views
I have been using ScrollView with SwiftUI to develop a Custom Table Structure due to certain Customisation limitations with ListView. As soon I as I add NavigationLink for each item in scrollView. The icons or Images in the items are highlighted to blue color. Is this an already reported bug or is it something which I should avoid doing?
Post not yet marked as solved
7 Replies
18k Views
Default TabView comes in light grey background color. the accentColor modifier works ok for changing the icon selected color, but I can not get the background color to change.Modifiers I've tried: .background(Color.white)This should work, but it doesn't. Could someone point me to the right direction?Thank you!
Posted
by
Post not yet marked as solved
3 Replies
5.3k Views
What is the best practice to achieve popToRootViewController like functionality in SwiftUI? There are couple of ways to do it and one of them is described nicely here (https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view) via publish event but it losses the animation while popping to root. Please suggest.
Posted
by
Post not yet marked as solved
2 Replies
1.2k Views
I followed SwiftUI Tutorials:https://developer.apple.com/tutorials/swiftui/building-lists-and-navigationand there s a methodpreviewdeviceto add device name to prview codeshttps://developer.apple.com/documentation/swiftui/view/3278637-previewdevicehere is my code:struct LandmarkList_Previews: PreviewProvider { static var previews: some View { ForEach(["iPhone SE", "iPhone XS Max", "iPad Pro (9.7-inch)", "Apple TV"], id: \.self) { deviceName in LandmarkList() .previewDevice(PreviewDevice(rawValue: deviceName)) .previewDisplayName(deviceName) } } }so when I do the preview, I can see iPhone, but I just see a blank page in iPad
Posted
by
Post marked as solved
2 Replies
3.8k Views
How does one remove the focus border for a TextField in SwiftUI?In Cocoa, setting the border to "None" in interface builder would remove the border.I've tried using .border with a width of zero, but that does not work. The .border adds another border on top of the focus border.Setting the style also does not do the trick.
Posted
by
Post not yet marked as solved
2 Replies
2.3k Views
How would remove the delete button in SwiftUI List rows when in Edit Mode? Note the hamburger button on the right of the row that allows rows to be re-ordered needs to continue to function.Background - Want a list that has the "re-order" rows functions always enabled. Edit mode seems to enable this (i.e. leave List in edit mode) however do not want the red delete button on each row.
Posted
by
Post marked as Apple Recommended
20k Views
So currently what I have been doing for determining what device type and orientation I am in for SwiftUI is using sizeClasses, usually something like the following:struct SizeClassView: View { @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass? var body: some View { if horizontalSizeClass == .compact && verticalSizeClass == .regular { Text("iPhone Portrait") } else if horizontalSizeClass == .regular && verticalSizeClass == .compact { Text("iPhone Landscape") } else if horizontalSizeClass == .regular && verticalSizeClass == .regular { Text("iPad Portrait/Landscape") } } }What I'd like to know is: Is there any better way to do this in SwiftUI? The main problem I'm having with this method is that I can't differentiate between iPad Portrait and Landscape, but at least I can use it to differentiate between iPhone Portrait and iPhone Landscape and iPad... Any help and insight is greatly appreciated!
Posted
by
Post not yet marked as solved
10 Replies
14k Views
Hi all!When presenting a specific modal sheet in a SwiftUI project, I consistently get a ton of console output like:=== AttributeGraph: cycle detected through attribute 290 ===Like, a couple hundred of these logs. It appears to be accompanied by a stutter/slowdown in the UI, but there's no crash and everything looks to be in the right place.Is the "cycle" here some sort of ARC retain cycle? Is it SwiftUI-specific? (I'm asking here because I've never seen this warning in a classic UIKit project.) Where can I go to learn more about this?Thanks!
Posted
by
Post not yet marked as solved
3 Replies
1.3k Views
I think I've found a bug with focusable(_: onFocusChange:) on MacOS with SwiftUI and wanted to sanity check it here before using feedback assistant. This is with Xcode 11.3.1When I create the window, the VStack inside thinks it is in focus. But the onFocusChange closure never gets called to say it loses focus. As a result, both my VStacks in both my windows think they are infocus.Demonstration project at https://github.com/darrellroot/focusBug/Here's my ContentView which displays whether it thinks it is in focus:struct ContentView: View { @State var inFocus = false let windowCount: Int var body: some View { VStack { Text("Focus Window \(windowCount)") inFocus ? Text("This window thinks it is in focus") : Text("This window does not think it is in focus") }.padding(50).focusable() { newFocus in debugPrint("onFocusChange: \(newFocus)") self.inFocus = newFocus } } }Here's my appDelegate code which spawns 1 window on launch and 1 more every time a particular menu is selected: var windows: [Int:NSWindow] = [:] var windowCount = 0 func applicationDidFinishLaunching(_ aNotification: Notification) { newWindow() } @IBAction func newFocusWindow(_ sender: NSMenuItem) { newWindow() } func newWindow() { windowCount = windowCount + 1 let contentView = ContentView(windowCount: windowCount) let window = NSWindow( contentRect: NSRect(x: 100, y: 100, width: 1000, height: 1000), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false) window.isReleasedWhenClosed = false windows[windowCount] = window window.title = "Focus Window \(self.windowCount)" window.tabbingMode = .disallowed window.center() //window.setFrameAutosaveName("Window \(self.windowCount)") window.contentView = NSHostingView(rootView: contentView) window.makeKeyAndOrderFront(nil) }DebugPrint output shows that onFocusChange got called once per window:2020-03-09 10:24:22.363001-0700 focusBug[2555:258395] Metal API Validation Enabled"onFocusChange: true""onFocusChange: true"
Posted
by
Post not yet marked as solved
4 Replies
833 Views
I'm trying to make an app that can send MIDI data over Bluetooth from my iPhone to my Mac. I'm using AudioKit as a framework for the MIDI aspect, and it's working great over a wired connection. I can't figure out how to implement MIDI over Bluetooth though.I found the CABTMIDILocalPeripheralViewController page here: https://developer.apple.com/documentation/coreaudiokit/cabtmidilocalperipheralviewcontroller, which looks promising, but I'm confused about how to use it.I'd really like to implement it using SwiftUI as opposed to UIKit, because the rest of the app is made with SwiftUI. Could someone please show me how I could use this in SwiftUI to make my iPhone discoverable as a Bluetooth MIDI device?I found some sample code written with UIKit, but I'd like to translate this to SwiftUI:import UIKit import CoreAudioKit import CoreMIDI class ViewController: UIViewController { var localPeripheralViewController:CABTMIDILocalPeripheralViewController? override func viewDidLoad() { super.viewDidLoad() localPeripheralViewController = CABTMIDILocalPeripheralViewController() } @IBAction func someAction(sender: AnyObject) { self.navigationController?.pushViewController(localPeripheralViewController!, animated: true) } }Thank you,Jack
Posted
by
Post not yet marked as solved
6 Replies
6.7k Views
I have an editor view that's broken up into several subviews. The model is a relatively small struct, but it does have a couple properties that can be variable-length arrays.The problem I'm having is after I add one entry to any one of these arrays, the view stops updating:struct ChecksView: View { @Binding var checks: [Check] var body: some View { HStack(alignment: .top, spacing: 0) { Button("add prereq", action: { self.checks.append(Check()) }) VStack { ForEach(self.checks.indices, id: \.self) { i in CheckView(check: self.$checks[i]) } } } } }In this subview, the first time you tap "add prereq", it will add to the array & update the UI. But every subsquent time, nothing updates until you navigate away & return. What am I doing wrong?
Posted
by
Post not yet marked as solved
4 Replies
3.4k Views
I’d like to know, how I can setup a toolbar inside a sheet using SwiftUI on macOS. I tried adding a NavigationView and used the following Code, but the top area of the sheet is always empty. .toolbar {                 ToolbarItem(placement: .confirmationAction) {                     Button(action: { //                      Do stuff                         self.presentationMode.wrappedValue.dismiss()                     }                     ) {                         Label("Done", systemImage: "checkmark.fill")                     }                 }                 ToolbarItem(placement: .cancellationAction) {                     Button(action: {                         self.presentationMode.wrappedValue.dismiss()                     }                     ) {                         Label("Cancel", systemImage: "checkmark.fill")                     }                 }             }
Posted
by
Post marked as Apple Recommended
1.6k Views
Does anyone know if there is a way to move the "Maps" and "Legal" text in the Map SwiftUI view? App Store Guidelines require that the text be shown, but if you are trying to add some type of sheet or view overlay for some of the map, I can't seem to find a way to add extra bottom padding to this text.
Posted
by
Post not yet marked as solved
5 Replies
6.7k Views
When I try to replicate the example from SwiftUI documentation - https://developer.apple.com/documentation/swiftui/toggle/onchange%28of%3Aperform%3A%29 on onChange modifier and capture the previous value, inside the closure the previous value is equal to the new value, not the old one. struct ContentView: View {   @State private var isSubscribed = false   var body: some View {     Toggle("Subscribe", isOn: $isSubscribed)             .onChange(of: isSubscribed) { [isSubscribed] newState in       print(isSubscribed)       print(newState)     }   } } In the documentation - https://developer.apple.com/documentation/swiftui/toggle/onchange%28of%3Aperform%3A%29 example, the onChange modifier is attached to the body property, not to a view inside it. Is this how it should be used? Attaching it to the body property in Xcode 12.0 beta (12A6159) doesn't compile.
Posted
by
Post marked as solved
5 Replies
6.8k Views
I'd like to emulate the behavior of UIViewController.isModalInPresentation in SwiftUI. In my first attempt, I defined the following view: struct ModalView<Content: View>: UIViewControllerRepresentable { 		var content: () -> Content 		func makeUIViewController(context: Context) -> UIHostingController<Content> { 				let controller = UIHostingController(rootView: content()) 				controller.isModalInPresentation = true 				return controller 		} 		func updateUIViewController(_ imagePickerController: UIHostingController<Content>, context: Context) {} } From my main app view, I then present the ModalView as a sheet: struct ContentView: View { 		@State 		var presentSheet: Bool = true 		var body: some View { 				Text("Hello, world!") 						.sheet(isPresented: $presentSheet) { 								ModalView { 										Text("Sheet") 								} 						} 		} } But the user is still able to dismiss the ModalView by swiping down. I would expect this sheet to be non-dismissible. Is anything like this supposed to work? If not, is there some other way to prevent the dismissal of a sheet in SwiftUI? The closest workaround I've found is to apply .highPriorityGesture(DragGesture()) to the content of the sheet, but swiping down with two fingers still works.
Posted
by