【问题标题】:UIPopoverController not moving to original position after keyboard slides back down键盘滑回后,UIPopoverController 未移动到原始位置
【发布时间】:2010-08-21 10:03:48
【问题描述】:

我在 iPad 中显示一个带有 UINavigation 栏的弹出框。在第二个视图中,我有一个可以显示键盘的 UISearchController。键盘将弹出框向上推,这很好,但是如果我现在按下 UINavigation 栏上的“后退”按钮,它会关闭键盘,这很好,但弹出框不会滑回原来的位置。有谁知道如何解决这个问题?谢谢!

【问题讨论】:

    标签: ipad uipopovercontroller


    【解决方案1】:

    好的,所以我实际上想通了(我相信)您的问题在问什么......万一有人从谷歌偶然发现这个问题,我想我会回答我是如何做到的。感觉就像是一项 hack 工作,但我找不到任何其他方法。

    在调出键盘的控制器中,我让它在键盘关闭时发布通知:

        [aTextField resignFirstResponder];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"movePopups" object:nil];
    

    然后回到我的主屏幕控制器,控制 UIPopover,我添加了一个监听器:

        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movePopUpToRightLocation)
                                                 name:@"movePopups"
                                               object:nil];    
    

    在初始化中。请务必记住删除您的 dealloc 中的侦听器以获得良好的编程习惯:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    因此,每当我收到键盘消失的通知时,我都会获得对弹出窗口显示的按钮的引用,然后直接从它重新出现:

    -(void)movePopUpToRightLocation {
    NSLog(@"move pop up to right location");
    if (morePopUp) {
        UIBarButtonItem *barButtonItem = (UIBarButtonItem *)[[bottomToolBar items] objectAtIndex:0];
        [morePopUp presentPopoverFromBarButtonItem:barButtonItem
                          permittedArrowDirections:UIPopoverArrowDirectionDown
                                          animated:YES];            
    }   
    

    }

    我没有添加任何检查它是哪个弹出窗口,但如果我有超过 1 种类型的弹出窗口/按钮会出现它,我可以轻松地做到这一点。但这是您可以使用的基本前提。

    希望对你有帮助!

    【讨论】:

    • 谢谢!这对我确定的人很有帮助。
    【解决方案2】:

    您还可以在初始化程序的某处注册 UIKeyboardDidHideNotification。

    [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movePopoverBack:) name:UIKeyboardDidHideNotification object:nil];
    

    此代码将弹出框移回:

    - (void)movePopoverBack:(id)sender {
        if ([self.settingsPopoverController isPopoverVisible]) {
            [self performSelector:@selector(hidePopover) withObject:nil afterDelay:0.1];
            [self performSelector:@selector(movePopoverBack) withObject:nil afterDelay:0.5];
        }
    }
    
    - (void)hidePopover {
        [self.settingsPopoverController dismissPopoverAnimated:YES];
    }
    
    - (void)movePopoverBack {
        [self.settingsPopoverController
         presentPopoverFromBarButtonItem:self.bottomToolbar.settingsButton
         permittedArrowDirections:UIPopoverArrowDirectionDown
         animated:YES];  
    }   
    

    我没有让它在没有延迟的情况下工作,但这对于我当前的项目来说似乎是可以接受的。希望它可以帮助某人。

    【讨论】:

      【解决方案3】:

      按下后退按钮后,您应该手动调用resignFirstResponder 来搜索搜索字段(例如在viewDidDisappear 中)。

      这应该会有所帮助,但是当设备处于横向且左侧有 ome 按钮时,问题仍然会在 iOS 4 下重现

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        相关资源
        最近更新 更多