1

I have one UIView. Inside that I have two text fields - username and password, and one submit UIButton. I kept my UIView at centre of screen. So now when I edit my username and password field, my password field is getting hide by keyboard.

So is it possible to show my "total UIView with that two text fields" up, when my keyboard is on?

Please help on some code.Thanks!

updated code:

@implementation ViewController
@synthesize scrollView;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, (self.scrollView.contentSize.height + 260));
    self.scrollView.contentOffset = CGPointMake(0,120);
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    self.scrollView.contentOffset = CGPointZero;
    self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
user5513630
  • 1,709
  • 8
  • 24
  • 48
  • 1
    This has already been answered – iarumas Feb 17 '16 at 17:01
  • Possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Bista Feb 17 '16 at 17:20

1 Answers1

0

Change your UIView to UIScrollView. Then in textFieldDidBeginEditing method add keyboard height (normally 260) to your scrollview's contentSize and change the contentOffset accordingly. Don't forget to reset the contentSize and contentOffset back to normal values in textFieldDidEndEditing

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, (self.scrollView.contentSize.height + 260));
    self.scrollView.contentOffset = CGPointMake(0, <desired height>);
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
     self.scrollView.contentOffset = CGPointZero;
     self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
Arun
  • 1,391
  • 1
  • 10
  • 29
  • i will chnage the uiview to uiscroll view. Then can you explain me that key board code and that chnage the contentoffset coding – user5513630 Feb 17 '16 at 17:03
  • your code is getting some name error for `desired` `loginRegisterView` `SCREEN_WIDTH , SCREEN_HEIGHT` – user5513630 Feb 17 '16 at 17:13
  • i just changed my uiview to uiscroll view – user5513630 Feb 17 '16 at 17:13
  • By desired height what I meant is the height which you want :D – Arun Feb 17 '16 at 17:54
  • height is for scrollview or for keyboard? – user5513630 Feb 17 '16 at 17:55
  • Set the contentOffset which you want something like self.scrollView.contentOffset = CGPointMake(0, 100); – Arun Feb 17 '16 at 17:58
  • i did, I just changed my uiview to uiscroll view.Now i add your code.But its same like looking. My scroll view is not moving up and also keyboard is not reducing height – user5513630 Feb 17 '16 at 18:02
  • Try increasing the content size. Actually size of keyboard won't decrease. This will make your scroll view scrollable so that you can see the field below keyboard by scrolling – Arun Feb 17 '16 at 18:05
  • content size?/There is no x,y, w , h declared in your solution to change the content size? – user5513630 Feb 17 '16 at 18:16
  • self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, (self.scrollView.contentSize.height + 260)); Increase 260 and check – Arun Feb 17 '16 at 18:17