3

I know this a frequent question but ive had no luck implementing this from examples i have found. i could be doing something simple wrong..(lets hope so). i am trying to move the UIview up as a have a textfield hidden at the bottom.

A little about my app, its got a tab bar controller and implements a standard UIView. When i add the code to move the display up nothing happens. Do i need to have a scroll view for this to work, is it a conflict with tab controller or am i doing something else wrong? thanks

Dan

Dan
  • 31
  • 1
  • 2
  • 3
    try posting some code of how you're moving the UIView up - could see where you're going wrong – Luke Mar 07 '11 at 14:25
  • http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present that is one tutorial but i have followed few now. It should be a case of modifying the textfield. – Dan Mar 07 '11 at 14:41
  • i keep getting warnings of 'MainViewController' may not respond to '-setViewMovedUp:' – Dan Mar 07 '11 at 14:42
  • You'll have to post the code that's handling the "moving up" of the view because your problem could be quite a few different things. – James Bedford Mar 07 '11 at 15:01

2 Answers2

6

I wrote a little category on UIView that manages temporarily scrolling things around without needing to wrap the whole thing into a UIScrollView. My use of the verb "scroll" here is perhaps not ideal, because it might make you think there's a scroll view involved, and there's not--we're just animating the position of a UIView (or UIView subclass).

There are a bunch of magic numbers embedded in this that are appropriate to my form and layout that might not be appropriate to yours, so I encourage tweaking this to fit your specific needs.

UIView+FormScroll.h:

#import <Foundation/Foundation.h>

@interface UIView (FormScroll) 

-(void)scrollToY:(float)y;
-(void)scrollToView:(UIView *)view;
-(void)scrollElement:(UIView *)view toPoint:(float)y;

@end

UIView+FormScroll.m:

#import "UIView+FormScroll.h"


@implementation UIView (FormScroll)


-(void)scrollToY:(float)y
{

    [UIView beginAnimations:@"registerScroll" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    self.transform = CGAffineTransformMakeTranslation(0, y);
    [UIView commitAnimations];

}

-(void)scrollToView:(UIView *)view
{
    CGRect theFrame = view.frame;
    float y = theFrame.origin.y - 15;
    y -= (y/1.7);
    [self scrollToY:-y];
}


-(void)scrollElement:(UIView *)view toPoint:(float)y
{
    CGRect theFrame = view.frame;
    float orig_y = theFrame.origin.y;
    float diff = y - orig_y;
    if (diff < 0) {
        [self scrollToY:diff];
    }
    else {
        [self scrollToY:0];
    }

}

@end

Import that into your UIViewController, and then you can do

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.view scrollToView:textField];
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    [self.view scrollToY:0];
    [textField resignFirstResponder];
}

...or whatever. That category gives you three pretty good ways to adjust the position of a view.

Dan Ray
  • 21,345
  • 6
  • 61
  • 86
1

The most simple way is placing your form in a UITableViewCell, and place the cell on UITableView. If then, UITableView will handle most of works automatically and exactly same with Apple apps does. I saw this on some other article or answer, but I cant remember where I saw this.

However I tried implementing mimic of UITableView's behavior, it was incredibly hard. Most of features easy, but I couldn't copy special behavior in rare cases. So I just chosen using UITableView.

eonil
  • 75,400
  • 74
  • 294
  • 482