键盘重新调整可减少弹出高度

时间:2011-04-01 12:31:08

标签: cocoa-touch ios

我在ios 4.3上有一个奇怪的问题。我有一个横向模式的屏幕,点击一个按钮就会出现一个popover.My popover有一个搜索栏。每当键盘出现时它会自动推出我的popover位。当我辞职时键盘,popover减少高度。这只是在ios 4.3上的问题。而在其余的ios中,我的popover在键盘解雇后不会降低高度。

6 个答案:

答案 0 :(得分:7)

上述答案都不适合我。显然,键盘缩放视图并在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]; 
}

答案 1 :(得分:3)

我在这里回答了一个非常相似的问题:UIPopoverController's view controller gets resized after keyboard disappears

我解决它的方法是观察键盘在控制器中消失,控制器控制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];
}

答案 2 :(得分:1)

我找到了答案。这是popover顶部箭头的一个错误。如果我使用左箭头方向进行弹出,一切正常。

答案 3 :(得分:1)

我也遇到了这个问题 - 具体来说,在弹出popover之后,popover没有恢复到它的键盘前尺寸。 (如果用户直接关闭键盘或者弹出窗口的视图控制器重新响应第一响应者,弹出窗口会重新生成。)

不幸的是,由于UI的布局,我必须使用顶部箭头方向来设置popover。要解决此问题,负责弹出窗口的视图控制器实现- (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;
}

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

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

答案 4 :(得分:0)

嗨在浏览论坛之后,我认为在使用帧大小之后这不是一个错误,在IOS 4,5,6,7上工作也是一样的行为。

我的解决方案是:

1)进入设计师 2)打开导致问题的XIB ViewController(即PopOver)。 3)单击以选择它的视图。 4)取消选中“AutoResizeSubviews” 5)在代码中加载PopOver时,请确保执行以下操作: 6)Your_Popup_Window.popoverContentSize = Your_ViewController.view.bounds.size;

我希望这会有所帮助。

亲切的问候 Heider Sati

答案 5 :(得分:0)

这是我的解决方案:

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

2. :创建局部变量:

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