简体   繁体   中英

Move the UIView with some UITextFields up when we edit UITextField

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);
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM