Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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 Objective-C:UIViewController中显示的键盘隐藏的UIScroller和UItextfields_Iphone_Objective C_Uiviewcontroller - Fatal编程技术网

Iphone Objective-C:UIViewController中显示的键盘隐藏的UIScroller和UItextfields

Iphone Objective-C:UIViewController中显示的键盘隐藏的UIScroller和UItextfields,iphone,objective-c,uiviewcontroller,Iphone,Objective C,Uiviewcontroller,我需要帮助尝试在UIViewController中配置键盘:(a)不要将鼠标悬停在两个UITextfields上,因此滚动条应该正确定位;(b)当用户触摸背景时,键盘消失 我还没有试过(b),但我正在试(a),我从谷歌搜索得到的代码没有给我想要的效果。事实上,当我触摸其中一个文本框时,它会使我的文本框消失。我确信我对activeField的实现也是错误的。我从Apple Development的键盘部分得到了这个示例 我目前掌握的代码如下: 我在一个博客条目中发现了以下代码(我不知道是哪一个,因

我需要帮助尝试在UIViewController中配置键盘:(a)不要将鼠标悬停在两个UITextfields上,因此滚动条应该正确定位;(b)当用户触摸背景时,键盘消失

我还没有试过(b),但我正在试(a),我从谷歌搜索得到的代码没有给我想要的效果。事实上,当我触摸其中一个文本框时,它会使我的文本框消失。我确信我对activeField的实现也是错误的。我从Apple Development的键盘部分得到了这个示例

我目前掌握的代码如下:
我在一个博客条目中发现了以下代码(我不知道是哪一个,因为我想在切换到Google Reader同步之前,我已经将其保存在NNW中了)。它都存在于实现文件中,只需将您的类作为UITextFields的委托,并在标头中声明为遵循UITextFieldDelegate协议即可。我直接从我的一个发货应用程序的代码中提取了这个,它非常适合将文本字段移出键盘。我还没有设置您的(B),但我相信这只是另一个需要实现的UITextFieldDelegate方法

为类设置以下常量:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;
然后添加这两种方法:

- (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 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;

   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);
    }
    CGFloat heightFraction = 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)textFieldDidEndEditing:(UITextField *)textField {
    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;
浮点分母=
(最大滚动分数-最小滚动分数)*viewRect.size.height;
CGFloat heightFraction=分子/分母;
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
动画距离=地板(横向键盘高度*高度分数);
}
CGFloat heightFraction=分子/分母;
如果(高度分数<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委员会];
}
-(void)textfielddidediting:(UITextField*)textField{
CGRect viewFrame=self.view.frame;
viewFrame.origin.y+=动画距离;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}

我认为解决此问题的最简单方法是根据键盘上下移动视图

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect rect = self.view.frame;
    rect = CGRectMake(rect.origin.x, rect.origin.y,
                  rect.size.width, rect.size.height + 80);//keyboard height
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = rect;
    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //same as above but you shift it down
}

我一直在寻找类似的东西,在我的例子中,密码字段被键盘隐藏了。我想用最少的代码行来解决我的问题

此解决方案还可以处理更多的文本字段,但只需检测每个字段并应用所需的偏移量

就我而言

1) 我首先将用户名、密码和登录按钮放在滚动视图中

2) 使viewController成为UITextFieldDelegate

3) 编写了以下代码行(确保您的scrollView是一个IBOutlet,并且它是从Interface Builder连接的)


正确的代码如下:

 static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
 static const CGFloat MINIMUM_SCROLL_FRACTION = 0.0;
 static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
 static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
 static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 240;

- (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 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;

  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);
  }
  heightFraction = numerator / denominator;

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

  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)textFieldDidEndEditing:(UITextField *)textField {
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];
}
静态常量CGFloat键盘\u动画\u持续时间=0.3;
静态常数CGFloat最小滚动分数=0.0;
静态常数CGFloat最大滚动分数=0.2;
静态常数CGFloat纵向键盘高度=216;
静态常数CGFloat横向键盘高度=240;
-(无效)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;
浮点分母=
(最大滚动分数-最小滚动分数)*viewRect.size.height;
CGFloat heightFraction=分子/分母;
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
动画距离=地板(横向键盘高度*高度分数);
}
高度分数=分子/分母;
如果(他)
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    if ([textField isEqual:textAccountPassword] == NO)
        return;

    CGPoint bottomOffset = CGPointMake(0, 20);
    [scrollview setContentOffset: bottomOffset animated: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    CGPoint bottomOffset = CGPointMake(0, 0); // return it back to no offset
    [scrollview setContentOffset: bottomOffset animated: YES];
}
 static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
 static const CGFloat MINIMUM_SCROLL_FRACTION = 0.0;
 static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
 static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
 static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 240;

- (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 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;

  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);
  }
  heightFraction = numerator / denominator;

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

  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)textFieldDidEndEditing:(UITextField *)textField {
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];
}