无法将imageView置于tableView页脚中心

时间:2014-08-20 21:34:43

标签: ios uitableview uiview cgrectmake

我正在尝试将标签和图像集中在tableView Footer

我似乎无法让它适用于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。

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

1 个答案:

答案 0 :(得分: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;
}

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