键盘滑回后 UIPopoverController 未移至原始位置

发布于 2024-09-15 13:38:43 字数 171 浏览 5 评论 0原文

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

I'm displaying a popover in iPad with a UINavigation bar. On the second view, I have a UISearchController that can display a keyboard. The keyboard pushes the popover up, which is fine, however if I now push the 'back' button on the UINavigation bar it dismisses the keyboard which is fine, but the popover doesn't slide back down to its original position. Anyone know how to fix that? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-09-22 13:38:43

好吧,所以我实际上弄清楚了(我相信)你的问题在问什么……以防万一有人从谷歌上偶然发现这个问题,我想我会回答我是如何做到的。这感觉像是一项黑客工作,但我找不到任何其他方法来做到这一点。

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

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

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

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

在 init 内部。请务必记住删除 dealloc 中的侦听器,以实现良好的编程实践:

[[NSNotificationCenter defaultCenter] removeObserver:self];

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

-(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 种类型的弹出窗口/按钮,我可以轻松地做到这一点。但这是你可以遵循的基本前提。

希望有帮助!

Ok so I actually figured out (I believe) what your question was asking...and just in case anyone stumbles upon this from google, I figured I'd answer how I did it. It feels like a hack job but I haven't been able to find any other way to do it.

In the controller that brings up the keyboard,I had it post a notification whenever the keyboard dismisses:

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

Then back on my home screen controller, that controls the UIPopover, I added a listener:

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

inside the init. Be sure to remember to remove the listener in your dealloc for good programming practice:

[[NSNotificationCenter defaultCenter] removeObserver:self];

So then whenever I get notification that the keyboard disappears, I get a reference to the button that the popover shows up from, and just have it re-appear directly from it:

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

}

I haven't added any checks for which popup it is, but I can easily do that if I have more than 1 type of popover / button that it would appear from. But that's the basic premise that you can go from.

Hope it helps!

居里长安 2024-09-22 13:38:43

您还可以在初始化程序中的某个位置注册 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];  
}   

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

You could also register for the UIKeyboardDidHideNotification somewhere in the initializer.

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

This code moves the popover back:

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

I didn't get it working without the delays, but this seems to be acceptable for my current project. Hope it helps someone.

薄凉少年不暖心 2024-09-22 13:38:43

按“后退”按钮后,您应该手动为搜索字段调用 resignFirstResponder(例如在 viewDidDisappear 内)。

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

After you press the Back button you should manually call resignFirstResponder for the search field (for example inside viewDidDisappear).

This should help, but the issue still will be reproduced under iOS 4 when the device is in Landscape orientation with Hthe ome button on the left side

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文