UIPopoverController not moving to original position after keyboard slides back down

百般思念 提交于 2019-12-10 03:05:36

问题


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!


回答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!




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/3537199/uipopovercontroller-not-moving-to-original-position-after-keyboard-slides-back-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!