0Day Forums
How can I make a UITextField move up when the keyboard is present - on starting to edit? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Objective-C (https://0day.red/Forum-Objective-C)
+--- Thread: How can I make a UITextField move up when the keyboard is present - on starting to edit? (/Thread-How-can-I-make-a-UITextField-move-up-when-the-keyboard-is-present-on-starting-to-edit)

Pages: 1 2 3


How can I make a UITextField move up when the keyboard is present - on starting to edit? - lithic983488 - 07-21-2023

With the iOS SDK:

I have a `UIView` with `UITextField`s that bring up a keyboard. I need it to be able to:

1. Allow scrolling of the contents of the `UIScrollView` to see the other text fields once the keyboard is brought up

2. Automatically "jump" (by scrolling up) or shortening

I know that I need a `UIScrollView`. I've tried changing the class of my `UIView` to a `UIScrollView`, but I'm still unable to scroll the textboxes up or down.

Do I need both a `UIView` and a `UIScrollView`? Does one go inside the other?

What needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the `UIView` (or `UIScrollView`) that I'm working with is brought up by a tabbar (`UITabBar`), which needs to function as normal.

----

I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the `UIScrollView` when the keyboard goes up and down. I'm simply using:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50); // Resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
// Keyboard will hide
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height + 215 - 50); // Resize
}

However, this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.




RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - Mrshirlineostv - 07-21-2023

RPDP's code successfully moves the text field out of the way of the keyboard. But when you scroll to the top after using and dismissing the keyboard, the top has been scrolled up out of the view. This is true for the Simulator and the device. To read the content at the top of that view, one has to reload the view.

Isn't his following code supposed to bring the view back down?

else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}



RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - Mrgsuit2 - 07-21-2023

Here is the hack solution I came up with for a specific layout. This solution is similar to Matt Gallagher solution in that is scrolls a section into view. I am still new to iPhone development, and am not familiar with how the layouts work. Thus, this hack.

My implementation needed to support scrolling when clicking in a field, and also scrolling when the user selects next on the keyboard.

I had a UIView with a height of 775. The controls are spread out basically in groups of 3 over a large space. I ended up with the following IB layout.

UIView -> UIScrollView -> [UI Components]

Here comes the hack

I set the UIScrollView height to 500 units larger then the actual layout (1250). I then created an array with the absolute positions I need to scroll to, and a simple function to get them based on the IB Tag number.

static NSInteger stepRange[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 140, 140, 140, 140, 410
};

NSInteger getScrollPos(NSInteger i) {
if (i < TXT_FIELD_INDEX_MIN || i > TXT_FIELD_INDEX_MAX) {
return 0 ;
return stepRange[i] ;
}


Now all you need to do is use the following two lines of code in textFieldDidBeginEditing and textFieldShouldReturn (the latter one if you are creating a next field navigation)

CGPoint point = CGPointMake(0, getScrollPos(textField.tag)) ;
[self.scrollView setContentOffset:point animated:YES] ;

An example.

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint point = CGPointMake(0, getScrollPos(textField.tag)) ;
[self.scrollView setContentOffset:point animated:YES] ;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {

NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

if (nextResponder) {
[nextResponder becomeFirstResponder];
CGPoint point = CGPointMake(0, getScrollPos(nextTag)) ;
[self.scrollView setContentOffset:point animated:YES] ;
}
else{
[textField resignFirstResponder];
}

return YES ;
}

This method does not 'scroll back' as other methods do. This was not a requirement. Again this was for a fairly 'tall' UIView, and I did not have days to learn the internal layout engines.


RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - preallotting829157 - 07-21-2023

Been searching for a good tutorial for beginners on the subject, found the best tutorial [here][1].


[1]:

[To see links please register here]


In the `MIScrollView.h` example at the bottom of the tutorial be sure to put a space at

@property (nonatomic, retain) id backgroundTapDelegate;

as you see.


RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - sidlers566940 - 07-21-2023

A much, much more elegant solution is to use a `UIView` subclass (though this isn't always appropriate) and recalculate all your subviews on a parent's frame change (and be smart: only recalculate them if the new frame size has changed, i.e. use `CGRectEqualToRect` to compare the new frame when you override `setFrame` and BEFORE you call `[super setFrame:frame_]`). The only catch to this is that the `UIViewController` you intend to use should probably listen to keyboard events (or, you could do it in the `UIView` itself, for handy encapsulation). But only the `UIKeyboardWillShowNotification` and `UIKeyboardWillHideNotification`. This is just so it will look smooth (if you wait for CG to call it, you will get a moment of choppiness).

This has the advantage of building a `UIView` subclass that does the right thing, anyway.

The naive implementation would be to override `drawRect:` (don't), a better one would be to just use `layoutSubviews` (and then in the `UIViewController`, or whatnot you can call [view `setNeedsLayout`] in a SINGLE method that is called for either show or hide).

This solution gets away from hardcoding a keyboard offset (which will change if they are not in split, etc) and also means that your view could be a subview of many other views and still respond properly.

Don't hardcode something like that unless there is no other solution. The OS gives you enough info, if you've done things right, that you just need to redraw intelligently (based on your new `frame` size). This is much cleaner and the way you <i>should</i> do things. (There may be an even better approach, though.)

Cheers.


RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - legislate254522 - 07-21-2023

Try this:

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:self.m_Sp_Contact])
{
[self.m_Scroller setContentOffset:CGPointMake(0, 105)animated:YES];
}
}



RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - pitsaw748 - 07-21-2023

Please follow these steps ,it might be helpful.
Put one view then put your textfield on that view and detect the event by delegate when your keyboard is coming up,at that time instant animate the view up(You can assign some position for that view also),then your view would be going up to that position.Do the same thing for animating the view down.


Thanks



RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - wacker818014 - 07-21-2023

As per [the docs][1], as of iOS 3.0, the `UITableViewController` class automatically resizes and repositions its table view when there is in-line editing of text fields. I think it's not sufficient to put the text field inside a `UITableViewCell` as some have indicated.

From [the docs][2]:

> A table view controller supports inline editing of table view rows;
> if, for example, rows have embedded text fields in editing mode, it
> scrolls the row being edited above the virtual keyboard that is
> displayed.


[1]:

[To see links please register here]

[2]:

[To see links please register here]




RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - Mrorganisation168 - 07-21-2023

There so many solutions, but I've spend some hours before it start works. So, I put this code here (just paste to the project, any modifications needn't):

@interface RegistrationViewController : UIViewController <UITextFieldDelegate>{
UITextField* activeField;
UIScrollView *scrollView;
}
@end

- (void)viewDidLoad
{
[super viewDidLoad];

scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

//scrool view must be under main view - swap it
UIView* natView = self.view;
[self setView:scrollView];
[self.view addSubview:natView];

CGSize scrollViewContentSize = self.view.frame.size;
[scrollView setContentSize:scrollViewContentSize];

[self registerForKeyboardNotifications];
}

- (void)viewDidUnload {
activeField = nil;
scrollView = nil;
[self unregisterForKeyboardNotifications];
[super viewDidUnload];
}

- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];

}

-(void)unregisterForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

- (void)keyboardWillShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

CGRect frame = self.view.frame;
frame.size.height -= kbSize.height;
CGPoint fOrigin = activeField.frame.origin;
fOrigin.y -= scrollView.contentOffset.y;
fOrigin.y += activeField.frame.size.height;
if (!CGRectContainsPoint(frame, fOrigin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y + activeField.frame.size.height - frame.size.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[scrollView setContentOffset:CGPointZero animated:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

P.S: I hope the code help somebody make desired effect quickly.
(Xcode 4.5)


RE: How can I make a UITextField move up when the keyboard is present - on starting to edit? - cadre867400 - 07-21-2023

Lots of answers here, but this works and is much shorter than most:


- (void)textFieldDidBeginEditing:(UITextField *)sender
{
UIScrollView *scrollView = (UIScrollView *)self.view; // assuming this method is pasted into the UIScrollView's controller
const double dontHardcodeTheKeyboardHeight = 162;
double textY = [sender convertPoint:CGPointMake(0, 0) toView:scrollView].y;
if (textY - scrollView.contentOffset.y + sender.frame.size.height > self.view.frame.size.height - dontHardcodeTheKeyboardHeight)
[scrollView setContentOffset:CGPointMake(0.0, textY - 10) animated:YES];
}