how to make a UITextField move up when the keyboard is presented


아래 답변대로 하되 매크로설정빼먹으면 오류~ 매크로수치만큼 뷰가 위로올라간다. 뷰가 움직이는 것이 감지되나 여전히 가리면 아래 매크로값을 조정하면 된다.

일반 뷰에 텍스트필드가 있을때에도 가능하다.



#define kOFFSET_FOR_KEYBOARD 120.0

With the iPhone SDK:

I have a UIView with UITextFields that brings 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? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then 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.


http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present


Edit: 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.
flag

12 Answers

9

1) You will need a scroll view if the contents you have now does not fit in iphone screen. [If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then its not needed]

2) For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown

here is a sample code


-(void)textFieldDidBeginEditing:(UITextField*)sender
{
if([sender isEqual:_textField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >=0)
{
[self setViewMovedUp:YES];
}
}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];// if you want to slide up the view

CGRect rect =self.view.frame;
if(movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect
.origin.y -= kOFFSET_FOR_KEYBOARD;
rect
.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect
.origin.y += kOFFSET_FOR_KEYBOARD;
rect
.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;

[UIView commitAnimations];
}


-(void)keyboardWillShow:(NSNotification*)notif
{
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

if([_textField isFirstResponder]&&self.view.frame.origin.y >=0)
{
[self setViewMovedUp:YES];
}
elseif(![_textField isFirstResponder]&&self.view.frame.origin.y <0)
{
[self setViewMovedUp:NO];
}
}


-(void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name
:UIKeyboardWillShowNotificationobject:self.view.window];
}

-(void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
}

define kOFFSET_FOR_KEYBOARD to a value as needed. like


#define kOFFSET_FOR_KEYBOARD 60.0

Hope this helps




Posted by 오늘마감

댓글을 달아 주세요

when textview empty, detecting it and disabling Button
In response to Victor Alexander on May 22, 2010 7:29 PM

This is what the 'delegate' property of the UITextField (or UITextView) is for. Then implement the appropriate delegate method so you get notified each time the field is changed. Then call your 'disabled' method from there.

In response to RickMaddy on May 22, 2010 11:47 PM

@RickMaddy thanks! So I'm really close... I know I'm missing something but I'm not quite sure what it is.

in my .h file I added my delegate to the view controller


UIViewController <UITextViewDelegate> 

I also added the following instance method that should tell the delegate that the text or attributes in the specified text view changed.


-(void)textViewDidChange:(UITextView *) textView1; 

In my .m file

I added a condition to the method above:


- (void) textViewDidChange:(UITextView *)textView1{      if (textView.text.length == 0) {           //disable button           moreButton.enabled = false;      }      else {           //enable button           moreButton.enabled = true;      } } 

I know that I'm missing something related to my delegate, but I'm not quite sure how to work this... even though I've read the documentation :/ Sorry to keep asking!


@property(nonatomic, assign) id<UITextViewDelegate> delegate 
In response to Victor Alexander on May 23, 2010 6:16 AM

"I'm not quite sure how to work this."

Two observations:

One, you have a typo.

You use textView1 as the parameter, and in the body of the method you use textView.

Two, connect the UITextView's delegate to you custom view controller class.

Try this:

  1. Open the view containing the UITextView.
  2. Double click on the View icon in the document window  to make sure the view is open in its editor window.
  3. Select the UITextView.
  4. Open the inspector to the Connections pane (cmd-2).

  5. Make a connection from the delegate to the File's Owner. Just click the delegate outlet and drag to the File's Owner icon the in the document window. Afte dragging you should see something like this:

  6. Save the nib file.
In response to Jeremy French on May 23, 2010 8:20 AM

Or if you are not using IB then in your view controller's 'viewDidLoad' method add the line 'textView.delegate = self'.







https://devforums.apple.com/message/223850

Posted by 오늘마감

댓글을 달아 주세요

how to make a UITextField move up when the keyboard is presented

With the iPhone SDK:

I have a UIView with UITextFields that brings 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? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then 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.

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present


Edit: 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.
flag

   

12 Answers

9

1) You will need a scroll view if the contents you have now does not fit in iphone screen. [If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then its not needed]

2) For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown

here is a sample code

-(void)textFieldDidBeginEditing:(UITextField*)sender
{
   
if([sender isEqual:_textField])
   
{
       
//move the main view, so that the keyboard does not hide it.
       
if  (self.view.frame.origin.y >=0)
       
{
           
[self setViewMovedUp:YES];
       
}
   
}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
   
[UIView beginAnimations:nil context:NULL];
   
[UIView setAnimationDuration:0.5];// if you want to slide up the view

   
CGRect rect =self.view.frame;
   
if(movedUp)
   
{
       
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
       
// 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect
.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect
.size.height += kOFFSET_FOR_KEYBOARD;
   
}
   
else
   
{
       
// revert back to the normal state.
        rect
.origin.y += kOFFSET_FOR_KEYBOARD;
        rect
.size.height -= kOFFSET_FOR_KEYBOARD;
   
}
   
self.view.frame = rect;

   
[UIView commitAnimations];
}


-(void)keyboardWillShow:(NSNotification*)notif
{
   
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

   
if([_textField isFirstResponder]&&self.view.frame.origin.y >=0)
   
{
       
[self setViewMovedUp:YES];
   
}
   
elseif(![_textField isFirstResponder]&&self.view.frame.origin.y <0)
   
{
       
[self setViewMovedUp:NO];
   
}
}


-(void)viewWillAppear:(BOOL)animated
{
   
// register for keyboard notifications
   
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
                                                                                         name
:UIKeyboardWillShowNotificationobject:self.view.window];
}

-(void)viewWillDisappear:(BOOL)animated
{
     
// unregister for keyboard notifications while not visible.
   
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
}

define kOFFSET_FOR_KEYBOARD to a value as needed. like

#define kOFFSET_FOR_KEYBOARD 60.0

Hope this helps



출처 : http://blog.naver.com/PostList.nhn?blogId=philipousys¤tPage=41
Posted by 오늘마감

댓글을 달아 주세요