当视图向上移动键盘时,UINavigationBar BarButtonItems不响应点击

时间:2012-09-17 18:17:41

标签: ios cocoa-touch uiview uinavigationbar uikeyboard

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

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

enter image description here

我进行了设置,以便在点击文本字段外时键盘应该关闭,但是当点击导航栏时这也不起作用。

当视图被偏移时,我如何恰当地响应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];

}

2 个答案:

答案 0 :(得分:3)

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

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

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

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

  3. 退房:

答案 1 :(得分:0)

我认为您的问题是您正在自己内部移动视图,而不是从外部移动视图。因此,屏幕上的视觉元素正在发生变化,但根据您的SuperView,视图位于同一位置,因此您只能在原始矩形内获得触摸事件(此矩形称为“边界”)。

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

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