0

我正在尝试在 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。

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

4

1 回答 1

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

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

于 2014-08-20T21:45:49.427 回答