jjzjj

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

coder 2024-01-20 原文

我的设计需要在应用程序的根以外的某个地方使用相当于 UISplitViewController 的东西。由于某些愚蠢的原因(感谢 Apple)这是非法的,我不得不手动重新编码它的某些方面。

表格在横向模式下布局正确,但当我将其移动到弹出窗口时,我遇到了一些奇怪的问题。最初,我的弹出窗口足够长,以至于它必须收缩才能为键盘提供空间,结果是 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];
}

最佳答案

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

您可以通过以下方式做到这一点:

  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 - 当键盘处于事件状态时,如何阻止我的 UITableView 在重新加载时不恰本地更改弹出窗口中的大小?的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  3. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. ruby - 在 Ruby 中重新分配常量时抛出异常? - 2

    我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案

  7. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. ruby - 是否可以在不实际发送或读取数据的情况下查明 ruby​​ 套接字是否处于 ESTABLISHED 或 CLOSE_WAIT 状态? - 2

    s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成

  9. ruby - 将全局 $stdout 重新分配给控制台 - ruby - 2

    我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r

  10. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

随机推荐