SwiftUI Form with Scrolling while Keyboard Displayed - forms

I have a SwiftUI Form with a field that is a TextField. I would like it when the keyboard appears for the Form to not be occluded by the keyboard AND for the field to be scrolled into view.
Form {
ForEach(0..<self.fields.count) { index in
CreateField(field: self.fields[index]) // this could be any of several input types
}
}
.modifier(KeyboardHandler())
where KeyboardHandler() is any one of several solutions found on Stackoverflow to change the bottom padding of the Form when the keyboard appears/disappears.
When you use a Form (which displays things very nicely) you can expect at least one item in the Form to require the use of a keyboard. I mean, that's one of the reasons to use a Form: gather info from the user. So it surprises me that keyboard handling is not built into the Form in some way. Or rather, in a way that works without us having to do this. There are a number of keyboard solutions on Stackoverflow - this is a wheel that has been reinvented more times than the wheel itself.
I have looked at several of the solutions presented and most will change the bottom padding of the Form so that it is not occluded by the keyboard. None of the solutions I've found address the issue of scrolling the field into view.
Is that possible with SwiftUI? Should I write my own custom Form (I'd rather not)?

Related

Can I customize keyboard layout for apple tv?

I want to have a custom keyboard (without full-screen keyboard) instead of default keyboard in my apple tv app?
If possible, what are the approaches should I follow?
Any suggestions appreciated.
The SearchTemplate is the only keyboard available for TVML, and there isn't a whole lot you can do with it.
But with TVML, there's 2 ways you can do redo the keyboard
1) using section with lockup and some TVJS, to create your own keyboard. Each lockup can be a character and onclick, can trigger tvjs to record and display the character into a label. Should be relative easy to implement, specially if you are only dealing with a small set of characters, like numbers.
2) custom element, as described in the docs, you can use native code to define any element and.. you can make your own keyboard that way.
=-=-=-=-=-=
Update
Somehow I missed the form template. This template included a keyboard too. And, you can *customize this keyboard!
The textField element provides a field for users to enter text, which also accepts the keyboardType attribute for selecting the type of keyboard to display. It even has support for number and URL inputs.
(*basic customization lol)

iPhone: keyboard blocks screen [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITableView and keyboard scrolling issue
I've got a screen which has several text fields (UITextField) for gathering information from the user which ends up taking up most of the screen. when the user selects a field the soft keyboard appears (as expected) but it covers up the fields that appear further down on the screen. In order to get to those fields the user has to close the keyboard and select them, and upon doing so, the field is covered up again and the user can't see what they're typing.
I tried wrapping the text fields in a UIScrollView, hoping that when the keyboard took up space on the screen i would be able to scroll the uppper portion of the screen down to the other text fields, but this did not work.
I'm hoping to get this scrolling behavior. Does anyone know how to do it?
-TIA
You need to adjust the height of your scroll view when the keyboard appears so that it doesn't overlap. See Managing the Keyboard in the programming guide.
There's a good answer to a similar question and some sample code here.
Basically, you register to receive the UIKeyboardWillShowNotification and the UIKeyboardWillHideNotification, then you manually scroll the view to compensate.
This is a drag and drop framework that I use in my projects as I have come across this problem a lot. It's a UIScrollView subclass and supports different auto-focus modes and a variety of other bits and bobs.
https://github.com/mackross/GTKeyboardHelper

TableView or ScrollView for edit form?

I wonder which will be the best route for build edit forms on the iPhone, using a TableView or using a scrollview.
I need:
Support up to 15 fields (similar to contact app)
The same behavior of safari forms, where is possible go back/forward among fields, and the form center the selected field and stay there when the user end the editing
Simple layout (one field after other)
I'm looking for the most-user friendly experience. Which route has been proved to be the better?
Exist good examples of great edit forms on iPhone apps?
I would vote for a tableview organized by sections. If nothing else, it is a more common layout and most users will be familiar with it.
In either case, you will have to handle the transition from field to field with custom code.

custom number pad

I want to create my own number pad to appear after user focus the textfield, so I have two question about it?
1.I use "Interface Builder to add a textfield in my view and select the "Number Pad" as the default pad for user to input number, so when I click the textfield, the number pad appear automaticlly, how can I stop it appear the number pad? because I want to show my number pad.
2.If I custom a view with number button inside it, how can I detect the event when I click the textfield? and whether after I detect the event I add a subview to show my custom number pad or not?
thanks
If you are creating a custom number pad (I assume this means a view with a grid of buttons), don't bother using a UITextField; there is no easy way to hide the native keyboard and, for all that trouble, there is nothing useful that the text field gives you.
I recommend creating a custom UIControl subclass. You can detect a touch inside the view and show your custom keypad that way. The documentation explains this pretty well.
I have a partial answer but not an ideal one, and I haven't tried this myself. Take a look at the documentation for the UITextFieldDelegate protocol.
You could have your controller set textField.delegate = self, then have it implement textFieldShouldBeginEditing to show your specialized keyboard somehow and then return NO so that it doesn't go into edit mode. When you tell your special keyboard to show itself, pass it a reference to the text field so it knows where to insert characters. The problem is that this probably won't show a cursor and won't let the user move the cursor to insert characters, etc.
So really this a bit ugly, but it may be sufficient. AFAIK there is no good way to do this :(

Large scrollview table with buttons

We are trying to write a training manual application for the iPhone. On the top half of the screen is a diagram of a car engine, on the bottom half is some text. At the user repeatedly hits a "next" button, we highlight different parts of the engine, and in concert we highlight different parts of the descriptive text below.
We basically want "living text" in the text half, with the illustration following along on top to where the reader is in the text. What we'd like from the text is 1. user can scroll it using their thumb so possibly a UIScrollView 2. the software can explicitly drive a scroll to any part of the text (when they hit the "next" button). 3. the words in the text are interspersed with hotlinks e.g. "this is the camshaft... this is the piston..." and the user should be able to click on any of the keywords like camshaft, piston, and have the diagram highlight that. (The problem is not highlighting the diagram, its capturing the click). The text would have 300~400 buttons/links/keywords and about 600 words of text.
Since this is fairly similar to using a web browser, we tried using Apple's version of webkit using a UIWebView and handleOpenURL to register a service back to the app itself. But Webkit for internal links a popup comes up asking permission to access that link. Every single the user wants to go to a link (in our case just an internal event that we'd intercept so that we can highlight e.g. the camshaft). Tried to intercept the event from the HTML view, but that didn't work.
It seems like the best we can do is to abandon scrolling text, and make the text part more like flash cards or a power point presentation, breaking the text into custom UIViewCells with buttons inside a UIScrollView. However, this would impose an annoying constraint on the author that they would have to write everything to fit in the UIViewCells, sort of chunky.
Any ideas would be appreciated.
This is definitely something you can use a UIWebView for. Don't use handleOpenURL, rather, set your viewController as the webview's delegate, and override -webView:shouldStartLoadWithRequest:navigationType:. When this gets called, check the request, and pull out your link data from there.
It would probably be easier to implement that completely in JavaScript in the document you load in a UIWebView. You would have to use JavaScript (i.e. [UIWebView stringbyevaluatingjavascriptfromstring:]) anyway to achieve things like scrolling to a certain position.