键盘退出降低了popup高度

我有一个奇怪的问题在ios 4.3上。我有一个我的屏幕在横向模式,button单击呈现popover.Mypopup窗口有一个search栏。每当键盘出现时,它会自动推动我popover位up.When我辞职的键盘, popover的高度降低。这只是在ios 4.3上的问题。而在其他的ios上,我的popover键盘解除后高度并没有降低。

上面的答案都没有为我工作。 显然,键盘会缩放视图,并在UIKeyboardDidHideNotification通知后恢复此缩放,使得在应用处理此通知时presentPopoverFromRect方法无用。 我解决这个问题的方法是延迟后者,如下所示:

- (void)viewDidLoad { [super viewDidLoad]; popup = nil; //my ViewController with UITextField popover = nil; //my UIPopoverController NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(resizePopup:) name:UIKeyboardDidHideNotification object:nil]; } - (void)doDelayedResize { [popover presentPopoverFromRect:myButton.bounds inView:myButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } - (void)resizePopup:(NSNotification*)note { [self performSelector:@selector(doDelayedResize) withObject:nil afterDelay:0.01]; } 

我在这里回答了一个非常类似的问题: 在键盘消失后,UIPopoverController的视图控制器被resize

我周围的方式是观察键盘消失在控制UIPopoverController的控制器中:

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

然后在-presentSearchPopover ,再次呈现UIPopoverController(这是一个相当无缝的过渡):

 - (void)presentSearchPopover { self.searchPopoverController.popoverContentSize = CGSizeMake(width, height)); [self.searchPopoverController presentPopoverFromRect:someRect) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } 

不要忘记在-dealloc或类似的地方删除观察者:

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; [super dealloc]; } 

我find了一个答案。这是一个popover的顶部箭头的错误。如果我使用左箭头方向popover,一切工作正常。

我也碰到过这个问题 – 具体地说,popover从popup窗口跳出后没有回到键盘前的大小。 (如果用户直接解散键盘或popup窗口的视图控制器退出第一响应者,则popup窗口会增长。

不幸的是,由于UI的布局,我必须使用顶部的箭头方向作为popup窗口。 为了解决这个问题,负责popup窗口的视图控制器实现- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 。 例如:

 @interface MyController : UIViewController <UIPopoverControllerDelegate> { // ... } //... @end 

将该控制器设置为popover的委托:

 MyPopoverViewController *popoverVC = [[MyPopoverViewController alloc] init]; UIPopoverController *myPopover = [[UIPopoverController alloc] initWithContentViewController:popoverVC]; myPopover.delegate = self; // Hang on to popoverVC, myPopover or release them as desired... 

另外,我的popover视图控制器将contentSizeForViewInPopover属性设置为所需的大小:

 @implementation MyPopoverViewController - (id)init { self = [super init]; if (self) { // ... self.contentSizeForViewInPopover = CGSizeMake(320, 400); // desired size } return self; } 

当键盘导致popup窗口缩小时,会影响popup窗口的popoverContentSize ,而不影响其视图控制器的contentSizeForViewInPopover 。 因此,在MyController的委托方法中重置popoverContentSize

 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { // Check if popoverController is valid, the popover you want, etc popoverController.popoverContentSize = popoverController.contentViewController.contentSizeForViewInPopover; } 

这是我的解决scheme:

1.注册键盘通知( UIKeyboardWillShowNotification, UIKeyboardWillHideNotification

2.创build局部variables:

 CGSize _currentPopoverContentSize; //if you want to have custom size for popover UIView *_currentPopoverSender; //to remember from wich view you will present popover BOOL _keyboardIsShown; //enable in keyboardWillShow, and disable in keyboardWillHide 

3.在我现在的翻页方法中:

 - (void)presentPopoverControllerWithSize:(CGSize)size fromView:(UIView *)sender{ MyController *controller = [[[MyController alloc] init] autorelease]; if (self.popover) { [_popover release]; _popover = nil; } _popover = [[UIPopoverController alloc] initWithContentViewController:controller]; _popover.popoverContentSize = size; _popover.delegate = self; //checking if keyboard is shown - if NO, than present popover, if YES - just `resignFirstResponder` for your _`activeTextField`(you can set it in -textFieldDidBeginEditing: and nullify in -textFieldDidEndEditing:) if (!_keyboardIsShown) { [_popover presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } else { [_activeTextField resignFirstResponder]; } _currentPopoverContentSize = size; _currentPopoverSender = sender; } 

4.比:

 - (void)keyboardWillBeHidden:(NSNotification*)aNotification{ [UIView animateWithDuration:0.3 animations:^{ //do some stuff [self.scrollView setContentSize:_scrollViewContentSize]; } completion:^(BOOL finished) { if (_popover && _currentPopoverSender) { [_popover presentPopoverFromRect:[_currentPopoverSender bounds] inView:_currentPopoverSender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } }]; _keyboardIsShown = NO; } 

嗨经过论坛,我不认为这是一个框架大小玩了很多,在IOS 4,5,6,7工作是一样的行为。

我的解决scheme是:

1)进入devise者2)打开导致问题的XIB ViewController(即PopOver之一)。 3)点击select它的视图。 4)取消选中“AutoResizeSubviews”5)在代码中加载PopOver时,请确保:6)Your_Popup_Window.popoverContentSize = Your_ViewController.view.bounds.size;

我希望这有帮助。

亲切的问候Heider Sati