【问题标题】:Unable to center a imageView in a tableView Footer无法在 tableView 页脚中居中 imageView
【发布时间】:2014-08-20 21:34:43
【问题描述】:

我正在尝试在 tableView 页脚中将标签和图像居中

我似乎无法让它同时适用于 iPhone 和 iPad 设备

现在它以 iPhone 为中心,但那是因为我对其进行了硬编码。

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

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
    [view setBackgroundColor:[UIColor clearColor]];
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.view.bounds.size.width, 20)];
    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    lbl.numberOfLines = 0;
    [lbl setText:@"Powered By"];
    [lbl setFont:[UIFont systemFontOfSize:10]];
    [lbl setTextAlignment:NSTextAlignmentCenter];
    [lbl setTextColor:[UIColor blackColor]];

    District *school = [District new];

    UIImageView  * logoView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 25, 150, 37.5)];
    logoView.image = [UIImage imageNamed:@"Logo.png"];

    [logoView autoresizingMask];
    [view addSubview:logoView];

    [view addSubview:lbl];

    return view;
}

我想将此视图居中,而不对其进行硬编码。我尝试将屏幕尺寸除以 2。

它没有居中,什么是正确的方法,请指教。

【问题讨论】:

  • 您需要正确设置标签和图像视图的autoresizingMask
  • 我在一个答案中看到了这个,我不知道如何添加它,你能告诉我吗,标签在图像上方,两者都应该居中
  • 尝试设置 logoView.center = view.center; 并且删除 [logoView autoresizingMask]; 它是一个吸气剂,你对此无能为力。

标签: ios uitableview uiview cgrectmake


【解决方案1】:

您需要根据需要设置视图的autoresizingMask

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
    [view setBackgroundColor:[UIColor clearColor]];

    CGRect lblFrame = view.bounds;
    lblFrame.origin.y = 15;
    lblFrame.size.height = 20;
    UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
    lbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    lbl.numberOfLines = 0;
    [lbl setText:@"Powered By"];
    [lbl setFont:[UIFont systemFontOfSize:10]];
    [lbl setTextAlignment:NSTextAlignmentCenter];
    [lbl setTextColor:[UIColor blackColor]];

    District *school = [District new];

    CGRect logoFrame = CGRectMake((view.bounds.size.width - 150) / 2.0, 25, 150, 37.5);
    UIImageView  * logoView = [[UIImageView alloc]initWithFrame:logoFrame];
    logoView.image = [UIImage imageNamed:@"Logo.png"];

    logoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [view addSubview:logoView];

    [view addSubview:lbl];

    return view;
}

这假设标签应该填充宽度并且图像应该保持从左到右居中。

【讨论】:

  • 如何设置logoView的中心?这样做不是更容易吗?
  • @JulianKról 这最初可以工作,但您仍然需要正确设置 autoresizingMask 以确保图像视图在其父视图大小发生变化时保持居中。
  • 好吧,那么你的更容易——少了一行代码:) 使用自动布局怎么样?这不应该是一条可以遵循的道路吗?
  • 非常感谢 rmaddy,我该如何练习以编程方式创建视图,这对我来说是 ios 的一个黑暗方面。任何想法
  • @WarehouseMuzik 通过这样做。我是相反的。 Storyboards 和 Interface Builder 让我困惑不已。我用代码做所有事情,因为它很简单,而且没有什么神秘之处。
猜你喜欢
  • 1970-01-01
  • 2018-01-15
  • 2011-05-12
  • 2016-07-22
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多