Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/45.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 键盘显示时在iPad上滑动UITextField_Iphone_Objective C_Ipad - Fatal编程技术网

Iphone 键盘显示时在iPad上滑动UITextField

Iphone 键盘显示时在iPad上滑动UITextField,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我有Cocoa with Love的代码,当键盘打开时滚动UITextField,这样键盘就不会覆盖UITextField 代码如下: - (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; CGRect viewRect = [self

我有Cocoa with Love的代码,当键盘打开时滚动UITextField,这样键盘就不会覆盖UITextField

代码如下:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;

    // Issue is that numerator isn't big enough for bottom 3rd of the screen

    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;

    CGFloat heightFraction = numerator / denominator;

    NSLog(@"Midline: %g Fraction: %g / %g", midline, numerator, denominator);

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline=textFieldRect.origin.y+0.5*textFieldRect.size.height;
//问题是分子不够大,不适合屏幕底部的三分之一
CGFloat分子=中线-viewRect.origin.y-最小滚动分数*viewRect.size.height;
CGFloat分母=(最大滚动分数-最小滚动分数)*viewRect.size.height;
CGFloat heightFraction=分子/分母;
NSLog(@“中线:%g分数:%g/%g”,中线,分子,分母);
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
动画距离=地板(横向键盘高度*高度分数);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=动画距离;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}
出于调试目的,我输出中线等,以查看发生了什么。这是从上到下的选项卡。您可以看到第六个分子变为负数,因为为其父视图转换的UITExtField.size.height是一个较低的数字。我不明白为什么这个数字会是负数。当你沿着视图往下走时,高度应该上升,就像所有其他的一样

2011-05-24 09:36:08.600婴儿花[27794:207]中线=246+0.5*167

2011-05-24 09:36:08.601婴儿布鲁姆[27794:207]中线:329.5分数:22.3/409.6

2011-05-24 09:36:09.535婴儿布鲁姆[27794:207]中线=246+0.5*167

2011-05-24 09:36:09.536婴儿布鲁姆[27794:207]中线:329.5分数:22.3/409.6

2011-05-24 09:36:09.929婴儿布鲁姆[27794:207]中线=246+0.5*246

2011-05-24 09:36:09.930婴儿布鲁姆[27794:207]中线:369分数:61.8/409.6

2011-05-24 09:36:10.313婴儿布鲁姆[27794:207]中线=246+0.5*246

2011-05-24 09:36:10.314婴儿布鲁姆[27794:207]中线:369分数:61.8/409.6

2011-05-24 09:36:10.793婴儿布鲁姆[27794:207]中线=246+0.5*97

2011-05-24 09:36:10.794婴儿布鲁姆[27794:207]中线:294.5分数:-12.7/409.6

2011-05-24 09:36:11.785婴儿布鲁姆[27794:207]中线=246+0.5*148


2011-05-24 09:36:11.786 Baby Bloom[27794:207]中线:320个分数:12.8/409.6

以下是使其工作的代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    float origin;
    float textFieldHeight;
    float viewOrigin;
    float viewHeight;

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];


    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        textFieldHeight = textFieldRect.size.height;
        origin = textFieldRect.origin.y;
        viewOrigin = viewRect.origin.y;
        viewHeight = viewRect.size.height;
    }
    else
    {
        textFieldHeight = textFieldRect.size.width;
        viewOrigin = viewRect.origin.x;
        viewHeight = viewRect.size.width;
        origin = viewHeight - textFieldRect.origin.x;
    }


    CGFloat midline = origin + 0.5 * textFieldHeight;

    // Take the current middle y location of the textfield
    // If it is in the top 20% of the view don't move it
    // If it is in the middle 60% move it by a fraction of the keyboard size
    // If it is in the bottom 20% move it by the whole size of the keyboard


    CGFloat numerator = midline - viewOrigin - MINIMUM_SCROLL_FRACTION * viewHeight;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewHeight;

    CGFloat heightFraction = numerator / denominator;


    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }



    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    //NSLog(@"Denom: %g", denominator);
    //NSLog(@"Distance: %g", animatedDistance);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
浮动原点;
浮动高度;
浮动视图原点;
浮动视图高度;
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
textFieldHeight=textFieldRect.size.height;
原点=textfieldcrect.origin.y;
viewOrigin=viewRect.origin.y;
viewHeight=viewRect.size.height;
}
其他的
{
textFieldHeight=textFieldRect.size.width;
viewOrigin=viewRect.origin.x;
viewHeight=viewRect.size.WITH;
原点=视图高度-textfieldcrect.origin.x;
}
CGFloat中线=原点+0.5*textFieldHeight;
//获取文本字段的当前中间y位置
//如果它位于视图的前20%,请不要移动它
如果它在中间60%,则按键盘大小的一小部分移动。
//如果它位于底部20%,则按键盘的整个尺寸移动它
CGFloat分子=中线-视图原点-最小滚动分数*视图高度;
CGFloat分母=(最大滚动分数-最小滚动分数)*视图高度;
CGFloat heightFraction=分子/分母;
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
动画距离=地板(横向键盘高度*高度分数);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=动画距离;
//NSLog(@“名称:%g”,分母);
//NSLog(@“距离:%g”,动画距离);
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}

结果表明,当在iPad上横向显示时,它会打开原点(x现在是y,宽度现在是高度)

以下是使其工作的代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    float origin;
    float textFieldHeight;
    float viewOrigin;
    float viewHeight;

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];


    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        textFieldHeight = textFieldRect.size.height;
        origin = textFieldRect.origin.y;
        viewOrigin = viewRect.origin.y;
        viewHeight = viewRect.size.height;
    }
    else
    {
        textFieldHeight = textFieldRect.size.width;
        viewOrigin = viewRect.origin.x;
        viewHeight = viewRect.size.width;
        origin = viewHeight - textFieldRect.origin.x;
    }


    CGFloat midline = origin + 0.5 * textFieldHeight;

    // Take the current middle y location of the textfield
    // If it is in the top 20% of the view don't move it
    // If it is in the middle 60% move it by a fraction of the keyboard size
    // If it is in the bottom 20% move it by the whole size of the keyboard


    CGFloat numerator = midline - viewOrigin - MINIMUM_SCROLL_FRACTION * viewHeight;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewHeight;

    CGFloat heightFraction = numerator / denominator;


    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }



    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    //NSLog(@"Denom: %g", denominator);
    //NSLog(@"Distance: %g", animatedDistance);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}
-(void)textFiel
origin = textFieldRect.origin.x;