I am a newbie to this! I am using Swift 4 and I have configured the plugin called SkyFloatLabelTextField.
I was wondering if someone else has solved how to auto move the label up if the cursor is on focus? I want the label to move up the moment you select a text field. At the moment you have to type for the label to move up.
Thank you.
You can achieve this functionality by subclassing SkyFloatingLabelTextField and managing title visibility.
final class MovingTitleOnFocusTextField: SkyFloatingLabelTextField {
override func becomeFirstResponder() -> Bool {
setTitleVisible(true)
return super.becomeFirstResponder()
}
override func resignFirstResponder() -> Bool {
setTitleVisible(hasText || hasErrorMessage)
return super.resignFirstResponder()
}
}
If you use SkyFloatingLabelTextFieldWithIcon you have to subclass that as well, but same idea.
UPDATE 2
I have forked the project and did the necessary changes. You can check it out here. I also did a Pull Request to the original author so this functionality will maybe be added to the original project.
To use the behavior simply set myFloatingTextField.isAnimationOnTouch 0 true. This will cause the animation to happen right when you tap in the textfield.
Happy coding!
UPDATE 1
So the question was more related to the behavior of the SkyFloatingLabelTextField. Sorry for misunderstanding!
Following the very first bunch of code on the github page, the label will move up as soon as you type. So you want it right from the touch?
Then you should fork the project, and adopt the code from
#objc open func editingChanged() {
updateControl(true)
updateTitleLabel(true)
}
to achieve it on the touch. This can be done in the delegate method func textFieldDidBeginEditing(_ textField: UITextField)
Once you have done this, I think you will be able to have the label move up once the user touches the textField.
Original Answer
This is not depended on the Labels you are using, the workflow is the same for every kind of view: The Keyboard appears and the textField should move up so the keyboard does not obscure the textField.
To achieve this, please have a look to this SO answer: Move textfield when keyboard appears swift
.
I don't want to copy the whole code in there, just point you into the right direction.
Happy coding!
Related
I'm a new programmer that's still learning, so forgive me if my code looks like crap. I'm writing a program that has a ViewController called "infoViewController" with multiple UITextFields on it. The user is supposed to fill out the textfields and then click the "Next" button in the upper-right of the screen, which then activates a "Show" segue and takes them to the next VC. All the info from the Text Fields are stored in constants for later down the road when I take that information that they inputted and place it on a .PDF file that is generated at the end. I wanted the user to be able to easily go through the text fields, so I made it where when the user is on the TextField and clicks "Next" on their keyboard, it moves them to the next TextField so that they can fill them all out quickly and easily. The issue I run into is that for some reason I keep getting this error when I click "Next" on the keyboard:
[Snapshotting] Snapshotting a view (0x7f89955786c0, _UIReplicantView) that has not been rendered at least once requires afterScreenUpdates:YES.
The app still works... but I'd rather not have an error showing up in the panel (in Xcode) at all lol. Also, when it does this, it's like the screen glitches and flickers white for a split second (super fast) before it moves on to the next TextField... any solutions?
Please dumb anything you reply down to a beginners level. Thanks!
Here's the code I wrote for switching between TextFields:
extension infoViewController {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextTag = textField.tag + 1
if let nextResponder = textField.superview?.viewWithTag(nextTag) {
nextResponder.becomeFirstResponder()
} else {
textField.resignFirstResponder()
}
return true
}
}
I assigned the TextField delegates in "viewDidLoad."
(Ex: clientTextField.delegate = self)
I also assigned the tags in "viewDidLoad."
(Ex: clientTextField.tag = 0)
I had the same warning whenever a textField switched firstResponders. But this only happened for textFields that had the Text Input Trait of Capitalization set to anything other than None.
Text Input Traits can be found in the Attributes Inspector under Min Font Size.
In my case, changing Text Input Trait of Capitalization to None removed that warning. While this worked for me, I imagine this only solves a symptom, not the underlying problem.
I would say, wrap your call to
nextResponder.becomeFirstResponder()
And also your call to
textField.resignFirstResponder
In a very brief delay to let the function return before you play with the first responder.
I have list of textfields inside stackviews which are again embedded in scrollview. For keyboard handling I've used IQKeyboard manager it works fine. Although there is a special case when it leaves too much space on top. Below is my current UI structure
Now suppose my cursor is on second textfield and I scrolled up till the end, now if I tap on the previous button, the focus does comes on first textfield but it also scrolls it down to show it just above keyboard like below screenshot. My actual expectation is, it should focus on first textfield like now but it should keep the top space to come down constant and shouldn't leave this much space.
From codewise I just enabled the IQKeyboardManager from AppDelegate. No other code is added for this.
IQKeyboardManager.shared.enable = true
Any help is appreciated
Try updating scroll view content offset on textFieldDidEndEditing like below
func textFieldDidEndEditing(_ textField: UITextField) {
scrollview.setContentOffset(CGPoint.zero, animated: false)
}
I'm having a hard time finding a solution to this so I thought I might as well ask to see if anyone has a better solution to this than I have. So to frame the question say I have a UI layout like so.
Where one focusable view is much larger than the other. And contains multiple valid focusable views underneath it when the user changes focus downward. Something like this.
My question is what is a good way to enforce that the button to the far left is focused as apposed to the other buttons.
So far the only solution I can think of is to override canBecomeFocused in a custom button subclass, and return the value of a settable variable.
class CustomButton: UIButton {
var isFocusable = true
canBecomeFocused: Bool {
return isFocusable
}
}
Then change the value of isFocusable based on where I want the focus to go next. It works but I'm not super happy with it. Just wondering if maybe someone else can think of a better solution.
You can check out UIKitCatalogtvOSCreatingandCustomizingUIKitControls example from Apple at https://developer.apple.com/library/archive/samplecode/UICatalogFortvOS/Introduction/Intro.html
Check out FocusGuide behaviour or shouldFocusAtItem and canFocusAtItem to get your preferredFocus behaviour.
Im trying to copy the content in a UILabel but without hiding the keyboard, the problem is that when I show the menu of copy in the label the label becomes first responder and the keyboard resign, but I want to do that without hiding the keyboard, is there a way to do that?
It is not possible with UILabel.
You should use UITextField for that. Just disable editing.
AFAIK, you can't do that. But I think you can have a work around for that. Instead of not hiding the keyboard, why don't keep track the current active text field and then active it after user press copy. You can use UIPasteboardChangedNotification to know when user pressed copy. For example:
self.lastActiveTextField = aTextField
-(void)pasteBoardDidChange:(NSNotification*)notif
{
[self.lastActiveTextField becomeFirstResponder];
}
I think so u r looking something like this project.
UILabel with UIKeyInput protocol implementation
https://github.com/hackiftekhar/IQEditableLabel
I think, it is not possible, there can be only one first responder at any time. If the keyboard is shown because of another UI element, then when you try to copy the content from UILabel, the OS has to transfer first responder from other element to UILabel, as there is no need of keyboard for UILabel, the keyboard will hide automatically. So, you have to make changes to your elements to fix this problem or use third party UI elements who can fix your problem.
Every UIView component has a method called: canBecomeFirstResponder. He is read only, but you can subclass the UI object and override the getter:
- (BOOL)canBecomeFirstResponder {
return false;
}
I didn't do the test, but if the "become first responder" is the problem, that should solve it.
I am new to stackoverflow and iOS app development using Swift. When I want to dismiss the keyboard in my apps I connect the uitextfield in viewcontroller.swift and create an action with the event DidEndOnExit. Then in that method I call the self.resignFirstResponder() method. This is what everybody says I should do. Here is my code in viewcontroller
#IBAction func dismissKeyboard(sender: AnyObject) {
self.resignFirstResponder()
}
Now when I was searching to find out what this method does I found that when I choose the textfield then the keyboard appears and that textfield becomes the first responder and by calling this method it resigns the first responder and the keyboard disappears. The thing is that if I don't use this method the keyboard still disappears. All I need to do is to create the action with the DidEndOnExit event. Am I missing something here? Here is the code without the method;
#IBAction func dismissKeyboard(sender: AnyObject) {
//self.resignFirstResponder()
}
If someone can enlighten me that would be great! :)
Thanks in advance!
maybe you enabled "Auto-enable Return Key" in the Interface builder?
The only thing that could come to mind is that you have an auto enabled return key. You also said it wasn't enabled, so maybe it has something to do with the rest of your code in the ViewController. Also, check any properties that you could have been playing with to get it to work, like you said you are new to swift so you we probably playing around with getting the keyboard to dismiss.
Sometimes Xcode can be a little buggy, if you can't solve the problem or get the right answer my advice is make a new project and add just the keyboard dismissing code in.
I hope this helps you out a little not seeing the rest of the code, screen shots and etc. make it so I can only give my thoughts or past experiences!
Good luck! Have fun programming in swift.