【问题标题】:UINavigationBar BarButtonItems don't respond to taps when view has been shifted up for keyboard当视图向上移动键盘时,UINavigationBar BarButtonItems 不响应点击
【发布时间】:2012-09-10 00:09:10
【问题描述】:

当显示键盘时,我将模式视图向上移动,以防止界面的某些部分被键盘隐藏。

当视图移动时,工具栏取消/保存按钮不会响应点击。检测到模态内的点击并响应良好。

我已将其设置为在文本字段外点击时键盘会关闭,但在点击导航栏时这也不起作用。

当视图被偏移时,如何正确响应对 barbuttonitems 的点击?

这是我在显示键盘时向上移动模式的方式:

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    int movementDistance;
    float movementDuration;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        if(
           UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
          )
        { 
            //code for landscape shift - not relevant because you can't see the toolbar
       else{
            NSLog(@"Portrait for animation");
            movementDistance = IPAD_PORTRAIT_KEYBOARD_HEIGHT; 
            movementDuration = KEYBOARD_ANIMATION_DURATION; 

            if(up){
                keyboardAppearedInLandscape = false;   
            }else{
                //the keyboard is going down
                NSLog(@"Keyboard going down");
                //is the iPad in the same orientation now that it was when it came up?
                if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) 
                {
                    if (!keyboardAppearedInLandscape) {
                        //don't do anything - the keyboard is being dismissed in the same way it was called. It's much the same in any case.
                    }else{
                        movementDistance = IPAD_LANDSCAPE_KEYBOARD_HEIGHT;
                    }
                }
            }
        }

    } 
    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}
//end text field movy-ness

这是我如何检测文本字段外的点击以关闭键盘:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {      
        //keyboard goes away if you tap somewhere else on screen
        NSLog(@"resignFirstResponder");
        [self.view endEditing:YES];

    }

    [super touchesBegan:touches withEvent:event];

}

【问题讨论】:

  • 您的触摸处理程序有干扰吗?即,如果您注释掉您的 touches[Began] 方法,工具栏按钮是否有效?
  • 不,他们没有 - 我可以完全删除该代码而不会产生任何影响(键盘关闭除外)。
  • 与您要移动的东西相关的工具栏在哪里?是不是也搬家了?您是否可能将视图移动到工具栏的顶部?
  • 导航栏是视图的一部分(即视图的“内部”)。工具栏顶部没有任何可见的东西,而且我没有任何透明元素。是的,导航栏向上移动。

标签: ios cocoa-touch uiview uinavigationbar uikeyboard


【解决方案1】:

您不会收到超出原始矩形边界的触摸事件,因为...

  • SuperView 认为视图没有改变位置
  • 视图在自身内部而不是从外部向上移动
  • 屏幕上的视觉元素正在发生变化,但未被识别

相反,要正确移动子视图:

  1. SuperView 应该回复 UIKeyboard
    • 利用UIKeyboardWillHideNotification && UIKeyboardWillShowNotification
  2. 设置ClipsToBounds to YES
    • 现在您不会看到超出视图范围的内容。
    • 让调试更有效

退房:

【讨论】:

    【解决方案2】:

    我认为您的问题是您将视图从内部向上移动,而不是从外部移动。所以,屏幕上的视觉元素在变化,但根据你的 SuperView,视图在同一个地方,所以你只会在原始矩形内获得触摸事件(这个矩形称为“边界”)。

    相反,您的 SuperView 应该响应 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 以将子视图移动到正确的位置。

    如果您将ClipsToBounds 设置为YES,您可以更轻松地调试此类东西。这将使您无法看到视图“边界”(可以接收触摸事件的区域)之外的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 2014-02-07
      • 2013-09-06
      • 2019-10-02
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多