ios - 当键盘处于事件状态时,如何阻止 UITableView 在重新加载时不恰本地更改弹出窗口中的大小?

标签 ios objective-c uitableview uipopovercontroller autolayout

我有一个设计,除了应用程序的根目录之外,还需要相当于 UISplitViewController 的东西。由于出于某种愚蠢的原因这是非法的(感谢苹果),我不得不手动重新编码它的某些方面。

表格在横向模式下布局正确,但是当我将其移动到弹出窗口时,我遇到了一些奇怪的问题。最初,我的弹出窗口足够长,以至于它必须缩小以为键盘提供空间,结果是 TableView 太大,最终被剪掉了。所以我缩小了弹出窗口......现在,当我重新加载数据时,TableView 正在缩小自己(当用户输入搜索键时我必须这样做)。请注意,该错误仅在我重新加载 tableView 后显示;现在它正在缩小,而不是剪切,而是在顶部和底部进行“带拳击”。

当我查询帧数据时,奇怪的是,tableView 似乎保持了它的高度。我不知道这意味着什么。如果我关闭弹出窗口并表示它,它并不能解决问题(我认为弹出窗口最终会变大?),但是当我回想起键盘时它会解决问题(弹出窗口缩小回正确的高度)。 (我不想尝试将其作为修复方法,因为这对于积极打字的用户来说只会很烦人)。

编辑:

如果重要的话,我应用的唯一自动布局是 UITableView;它被赋予了固定的宽度和高度。没有 X 或 Y 数据,这可能是一个错误,除非我在尝试引用 super View 时生成错误 - 可能是因为弹出窗口在我尝试呈现它之前不会创建 super View ?

编辑:请求的代码(抱歉,这是一个又大又难看的 block ):

-(void)setupViewsAfterRotation
{
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
        [self.searchTable.view removeFromSuperview];
        self.popover=[[UIPopoverController alloc] initWithContentViewController:self.searchTable];
        self.navigationItem.leftItemsSupplementBackButton=YES;
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Search"
                                                                               style:UIBarButtonItemStylePlain
                                                                              target:self
                                                                              action:@selector(presentPopover)];
        [self setupPortraitConstraints];
        //NSLog(@"Intrinsic size data:  width: %f and height: %f",self.searchTable.view.intrinsicContentSize.width, self.searchTable.view.intrinsicContentSize.height);
        //NSLog(@"Runtime size data:  width: %f and height: %f",self.searchTable.view.frame.size.width, self.searchTable.view.frame.size.height);

    } else {
        [self.popover dismissPopoverAnimated:NO];
        self.popover=nil;
        self.navigationItem.leftBarButtonItem=nil;
        [self.view addSubview:self.searchTable.view];
        [self setupLandscapeConstraints];
    }
}

-(void)setupLandscapeConstraints
{
    if (self.tableViewConstraints) {
        [self.view removeConstraints:self.tableViewConstraints];
        self.tableViewConstraints=nil;
    }
    NSMutableArray *landscapeConstraints=[[NSLayoutConstraint constraintsWithVisualFormat:@"|[tableView(==256)]"
                                                                                  options:0
                                                                                  metrics:nil
                                                                                    views:@{@"tableView": self.searchTable.view}] mutableCopy];

    [landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide][tableView]|"
                                                                                      options:0
                                                                                      metrics:nil
                                                                                        views:@{@"tableView": self.searchTable.view,
                                                                                                @"topLayoutGuide":[self topLayoutGuide]
                                                                                                }]];
    self.tableViewConstraints=landscapeConstraints;
    [self.view addConstraints:self.tableViewConstraints];
}

-(void)setupPortraitConstraints
{
    if (self.tableViewConstraints) {
        [self.view removeConstraints:self.tableViewConstraints];
        self.tableViewConstraints=nil;
    }
    NSMutableArray *portraitConstraints;
    if (self.keyboardHeight) {
        NSLog(@"Height set to 612");
        portraitConstraints=[[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tableView(==612)]"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:@{@"tableView": self.searchTable.view}] mutableCopy];
        [self.popover setPopoverContentSize:CGSizeMake(256, 612) animated:YES];
    }
    else{
        NSLog(@"Height set to 768");
        portraitConstraints=[[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tableView(==768)]"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:@{@"tableView": self.searchTable.view}] mutableCopy];
        [self.popover setPopoverContentSize:CGSizeMake(256, 768) animated:YES];
    }

    [portraitConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[tableView(==256)]"
                                                                                     options:0
                                                                                     metrics:nil
                                                                                       views:@{@"tableView": self.searchTable.view}]];
    self.tableViewConstraints=portraitConstraints;
    [self.searchTable.view addConstraints:self.tableViewConstraints];
}

最佳答案

设置弹出窗口的内容大小以限制弹出窗口的查看区域。

您可以通过以下方式执行此操作:

  1. 使用 setPopoverContentSize: 方法设置弹出窗口的内容大小。
  2. 设置内容 View Controller 的 contentSizeForViewInPopover。

来自Apple's Documentation :

Popovers normally derive their size from the view controller they present. However, you can change the size of the popover by modifying the value in the popoverContentSize property or by calling the setPopoverContentSize:animated: method. The latter approach is particularly effective if you need to animate changes to the popover’s size. The size you specify is just the preferred size for the popover’s view. The actual size may be altered to ensure that the popover fits on the screen and does not collide with the keyboard.

来自Apple's documentation关于 popoverContentSize 属性:

Changing the value of this property overrides the default value of the current view controller. The overridden value persists until you assign a new content view controller to the receiver. Thus, if you want to keep your overridden value, you must reassign it after changing the content view controller.

关于ios - 当键盘处于事件状态时,如何阻止 UITableView 在重新加载时不恰本地更改弹出窗口中的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18842245/

相关文章:

ios - 将表格 View 单元格中的颜色 slider 设置为单元格的全长

android - 无法在 iOS 中点击 Admob 横幅广告

objective-c - 类别对键@sum的键值编码不兼容

ios - Autorotation 仅适用于模拟器而不适用于设备(​​IOS 7.1)

ios - 如何让 UIlabel 动态地自动跨越多行。在具有多个 UIlabels 的 Tableview 单元格/ContentView 中

ios - TableView 的单元格未显示

ios - 如何隐藏我的 UITableViewCell 之外的内容

ios - 媒体查询不适用于 iphone 5 横向

ios - Swift - 帮助从 Objective C 语法转换

ios - 视频无法在 iPhone 上播放