开发者

I have a UITextField that gets hidden by the keyboard when selected. How can it be brought into view?

开发者 https://www.devze.com 2023-03-12 20:29 出处:网络
I have 开发者_运维百科a UITableView that is shorter than the window, therefore it does not need to scroll. However, it is long enough that when a text field in the bottom row is selected, the keyboard

I have 开发者_运维百科a UITableView that is shorter than the window, therefore it does not need to scroll. However, it is long enough that when a text field in the bottom row is selected, the keyboard covers it.

I can't use scrollToRowAtIndexPath because the table is shorter than the window, so I was wondering what the correct way to bring it into view would be.

I was thinking about sliding the whole view up a set number of pixels, although that seems very bad form because it would break the UI if I added more rows to the table.


You should implement these methods in the concerned class :

- (void) textFieldDidBeginEditing:(UITextField *)myTextField
{
    [self animateTextField:myTextField up:YES];
}

- (void) textFieldDidEndEditing:(UITextField *)myTextField
{
    [self animateTextField:myTextField up:NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    int movement = (up ? -105 : 105);

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

You have to adapt values (-105, 105 and 0.3f) to your situation.


You can have the whole tableView slide up by setting the height of the footerView. The Keyboard will move the table above for the height of the footer

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 70.0;
}


Pierre's code worked for me, but I removed the myTextField argument. It seemed unnecessary. I also changed this over to UITextViews because that's what I had in my code. Otherwise, thanks for the question and answers. I've been beating my head against the wall to solve this problem!

#pragma mark - Text View Delegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self animateTextViewUp:YES];
}

- (void) textViewDidEndEditing:(UITextView *)textView
{
    [self animateTextViewUp:NO];
}

- (void) animateTextViewUp:(BOOL)up
{
    int movement = (up ? -80 :80);

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号