草庐IT

ios - 如何在支持方向的情况下将 UITableView 部分页脚居中?

coder 2024-01-15 原文

编辑: 在弄乱了几天之后,我真正的问题如下: 1. UITableView是否占满整个view?
2. 如果是这样,它如何将单元格的边界设置为看起来只占据 View 的一部分。 3. 我如何获得单元格的边界——或者更准确地说,我如何知道单元格占据的可见区域的边界。 self.tableView.bounds.size.width 没有帮助,因为它返回 View 的宽度。 谢谢。 将之前的信息留在下面,以防它有助于使我的问题更清楚。

这可能吗? 我已经阅读了苹果文档并浏览了这里和其他地方的论坛,但无法找到并回答这个问题。 无论您做什么,UITableVIew 中的页脚是否实际上占据了整个 View ?它没有表格宽度的概念吗?

例子:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    [footerView setBackgroundColor:[UIColor redColor]];

    return footerView;
}

此代码将创建一条从一条边到另一条边的红线。无论你给它什么边界,这条线都会占据整个视野。这样做的问题是,如果你想将标签放在该页脚的中心,如果你支持方向更改,你将无法知道中心在哪里。 例如,在 iPad 应用程序中,我正在尝试执行以下操作:

if ([footerText length] > 0) {
    UIView *customView = [[[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 0, 0.0)] autorelease];

    [customView setBackgroundColor:[UIColor grayColor]];

    UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    footerLabel.numberOfLines = 2;
    footerLabel.adjustsFontSizeToFitWidth = NO;
    [footerLabel setTextAlignment:UITextAlignmentCenter];
    [footerLabel setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
    [footerLabel setOpaque:NO];
    [footerLabel setTextColor:[UIColor whiteColor]];
    [footerLabel setShadowColor:[UIColor lightGrayColor]];
    [footerLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [footerLabel setFrame:CGRectMake(customView.center.x/0.3, 0.0, 600, 40.0)];
    [footerLabel setText:footerText];
    [customView addSubview:footerLabel];
    [footerLabel release];

    NSLog(@"customView width = %f", customView.frame.size.width);
    NSLog(@"tableview width = %f", self.tableView.frame.size.width);
    NSLog(@"tableview center = %f", self.tableView.center.x);

    return customView;
} else {
    return nil;
}

表格的中心在纵向时应为 384(在详细 View /右侧),在横向时应为 351.5。但是当我使用 setCenter 或尝试根据该中心调整左边缘时,它不会居中。

最后一个问题:当页脚似乎没有表格边界的概念时,如何在支持方向的页脚中居中自定义 View ?我一定是在文档中遗漏了一些东西,因为这必须是其他人解决的问题,但我找不到它。 感谢您的宝贵时间。

最佳答案

要在 TableView 中居中放置某些内容,您需要将其包装在容器中,并为嵌入式 View 和容器设置适当的自动调整大小掩码。

容器应该是灵活的宽度,嵌入式 View 应该有两个灵活的侧边距。

例如:

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    static UIView *footerView;

    if (footerView != nil)
        return footerView;

    NSString *footerText = NSLocalizedString(@"Some Display Text Key", nil);

    // set the container width to a known value so that we can center a label in it
    // it will get resized by the tableview since we set autoresizeflags
    float footerWidth = 150.0f;
    float padding = 10.0f; // an arbitrary amount to center the label in the container

    footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, footerWidth, 44.0f)];
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // create the label centered in the container, then set the appropriate autoresize mask
    UILabel *footerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)] autorelease];
    footerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    footerLabel.textAlignment = UITextAlignmentCenter;
    footerLabel.text = footerText;

    [footerView addSubview:footerLabel];

    return footerView;
}

关于ios - 如何在支持方向的情况下将 UITableView 部分页脚居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458373/

有关ios - 如何在支持方向的情况下将 UITableView 部分页脚居中?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  4. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  5. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  6. 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

  7. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  8. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  9. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  10. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

随机推荐