How do we add "InputAccessoryItem"s in Swift UI? - swiftui

Using SwiftUI and running into an issue with the keyboard. I want to use the .deicmalPad but I can't dismiss it since it has no "return" button.
Found a workaround: use UIApplication.shared.windows[0].endEditing(true) on a button. (Tried using it on the viewController for the app but it broke NavigationLinks)
Now I want to add the "Next", "Previous" and "Submit" strip above the keyboard, like in this picture: http://brainwashinc.com/wp-content/uploads/2017/07/Simulator-Screen-Shot-Jul-28-2017-9.07.42-AM.png
can't find SwiftUI documentation for this feature, only finding incompatable UIKit solutions involving inputAccessoryView and it's items.
Ultimatly want to use the "Submit" button's event to use UIApplication.shared.windows[0].endEditing(true) and finally get the keyboard to go away.
Thanks in advance

Related

What are some alternatives to NavigationLink for a macOS app?

I want to go from one view to another with the click of a button. Like if I press Log In, it goes to the "Home" view.
I tried to use NavigationLink, but it needs a NavigationView, which I don't want to do because it messes with the format of the app on macOS. What is an alternative to NavigationLink to get from one view to another?

SwiftUI DatePicker in Form Dismisses When Keyboard Appears

I have a SwiftUI Form with a Section that contains a DatePicker. The DatePicker is set to have a datePickerStyle of CompactDatePickerStyle(), by default, since it's in a Form.
When tapping on the DatePicker, the overlay is presented:
The DatePicker's time is able to be modified by using a gesture, as seen in the following video:
In the following video, tapping on the overlaid DatePicker's time to modify it via the keyboard causes the overlay to be dismissed:
I have also tried adding the following to the DatePicker in order to allow for inline date manipulation, hoping for keyboard avoidance:
.datePickerStyle(GraphicalDatePickerStyle())
.ignoresSafeArea(.keyboard, edges: .bottom)
However, the above results in the following:
What do I need to change to allow the DatePicker to be manipulated via gestures and keyboard input within the form?
You need to raise the datepicker so the keyboard wouldn't show over the datepicker, already reported this bug in October 2020 and the ticket is still open. These datepicker don't have keyboard avoidance. Also found an issue with GraphicalDatePickerStyle that can crash the app, ticket still open too.
Did you try to use .ignoresSafeArea(.keyboard) on the entire Form? I had the same issue and this fixed it for me.

SwiftUI add sound effect when a button is pressed

I would like to add a sound effect when a button is pressed in my app for SwiftUI. The button has another feature, but I just want to add a sound effect. I have found information for previous versions of Xcode using UIkit, but I'm not understanding how to do it in SwiftUI.
Edit: I'm very new to programming if that explains my lack of knowledge.
from answer:
Play reminder sound SwiftUI
you can use this simple method:
import AVFoundation
AudioServicesPlaySystemSound(1026)
where the number 1026 is the SystemSound id.

iOS SwiftUI how to bring up extra actions like "Embed in VStack" when interacting with code?

I am looking at this SwiftUI tutorial and it suggests I can see extra actions upon Command-Clicking the SwiftUI elements in code. In my Xcode, this jumps me to the class definition.
I see there's an option in XCode 11.0 t change the Option key to show SwiftUI inspector, however, this still does not let me modify SwiftUI code elements using actions like "Embed in VStack"
How can I get both inspector and extra SwiftUI actions when interacting with SwiftUI Code (not the preview)?
Extra SwiftUI actions show when Canvas is opened (shortcut: Option-Command-Enter or ⌥+⌘+↵):
canvas is closed
canvas is opened
The accepted answer doesn't work for me on macOS 10.15.5 and Xcode 11.4.1.
The Good news is that you can get it via a shortcut of [Command][Shift][Left Click] on a SwiftUI element or by clicking Show Code Actions on the right click menu.
Instead of using the mouse, I recommend using the Xcode hotkey for bringing up code actions. Navigate to Preferences -> Key Bindings and search for Show Code Actions. You'll see the preset hotkey which you can then customize. My default key binding was Shift+Command+A. Just make sure you cursor is over the intended element when you press the hotkey.
You can change the command click behavior where you were looking under Xcode -> Preferences, Navigate tab.
Rather unintuitively, if you pick "Selects Code Structure", then command+click will bring up the SwiftUI menu options you're looking for (granted the canvas is open).
If you do this, you can still jump to definition via control+command+click
Conversely, if you'd prefer to continue to use command+click to jump to definition, you can instead use control+command+click to bring up the SwiftUI menu options.
Command clicking on a view will not work in Xcode 12.2 and 12.3 (under MacOS 10.15.5) if you have your preferences different than Xcode expects.
You have to change your Navigation preferences for Command-click on Code: to Selects Code Structure before the proper menu will appear when you command click on a View.
To change this. press command , to open your preferences and select the Navigation button. The option you need to change is in the first popup menu.

how to get previously selected tab after dismissing modal IOS

I have an application using UITabBarController. There are 5 tabs (tab1, tab2, cameraTab, tab3, tab4). When user taps on middle tab , the modal view will appear hiding the TabBar (in my case camera - UIImagePickerController). If user then taps on cancel button on that modal view, how do I make it to return to previously selected tab. For example: if i am on tab2 and I tap on the camera tab, modal view appears and then I tap on cancel, how do i return back to tab2 automatically. The same goes for all other tabs, if I'm on tab3 it should return to tab 3 and so on. Right now it remains on camera tab without modal view - just blank view with background image.
I'd really appreciate if you can help me with some examples. I've been searching in various ways - there must be a way that UITabBarController keeps a record of previously selected tab.
Thank you
Personally, I think you should create a custom view where the middle camera tab would not be a tab at all, it would actually just be a button that presents the modal view and then that view would have a button to dismiss it. I would do a little research in http://idevrecipes.com/ to see if there is anything there. If not, maybe github. The tab bar itself is looking for views to push, not present.
I am sorry I couldn't give you more of an answer, but I wanted to give you something for waiting so long.