开发者

Easiest way to prevent the cursor from going behind the keyboard in iPhone SDK?

I have a table with several text fields in it. Tapping on any of the UITextFields brings up the keyboard and the view scrolls automatically to bring the cursor above the keyboa开发者_JS百科rd if required.

I have a method in there that gets called whenever the user presses the Return key while editing a text field and I simply move the first responder status to the next text field in that. However, if it happens to be behind the keyboard, it does not scroll up and reveal itself.

I know this involves using the beginAnimations method of UIView and changing the position or size of the view but I don’t know how to calculate how much the view should move, etc.? Is there an easy way to do this? I don’t think Apple has explained this in the documentation and I’ve already tried Googling, but couldn’t come up with a decent solution.

Any help would be greatly appreciated.

Thank you.


Found a solution to this, thanks to ‘mvds’ here and some digging around in the documentation. This is what my revised ‘scrollViewToCenter’ method looks like:

- (void)scrollViewToCenter
{
    [self determineActiveTextField];
    CGPoint textFieldOrigin = [activeTextField convertPoint:activeTextField.frame.origin toView:self.view];
    CGFloat scrollPoint = self.view.frame.size.height / 2 - activeTextField.frame.size.height;
    if (textFieldOrigin.y > scrollPoint) {
        CGFloat scrollDistance = textFieldOrigin.y - scrollPoint;
        [self.tableView setContentOffset:CGPointMake(0.0f,scrollDistance) animated:YES];
    }
}

I call it whenever the user taps into any of the text fields (or it has been made the first responder programmatically) by adding it as a selector for the ‘UITextFieldTextDidBeginEditingNotification’ notification thusly:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollViewToCenter) name:UITextFieldTextDidBeginEditingNotification object:textField];

When the method is called, it first determines which of the seven text fields is the active one and then, using the ‘convertPoint:toView:’ method, converts its origin point to a point on ‘self.view’. Once I have the point where the text field originates from, I calculate if it is below the halfway point on the screen and, if so, offset the table view accordingly using the ‘setContentOffset’ method.

I’ve posted this in so much detail because I had to go to great lengths to find out how this works and I figured it might save someone else that time. I hope it was useful.


AFAIK there is indeed no developer friendly way to do this. Copy&pasting from another project, this is what I came up with:

-(void)scrollViewToCenter:(UIView*)v
{
    CGFloat windowLeft = 200.0f;
    CGFloat y = v.frame.origin.y-(windowLeft - v.frame.size.height)/2;
    if ( y > scrollView.contentSize.height - windowLeft )
    {
        y = scrollView.contentSize.height - windowLeft;
    }
    if ( y < 0.0f ) y = 0.0f;
    [scrollView setContentOffset:CGPointMake(0.0f,y) animated:YES];
}

windowleft is the number of px left when the keyboard comes out. This assumes you have a UIScrollView called scrollView.

And then just call

[self scrollViewToCenter:tf];

edit: Note: for this to work, the UIView passed should preferably be a subview of the scrollView.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜