gpt4 book ai didi

ios - 无法在 tableView 页脚中将 imageView 居中

转载 作者:行者123 更新时间:2023-11-29 02:43:39 25 4
gpt4 key购买 nike

我正在尝试将标签和图像置于 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;
}

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

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

最佳答案

您需要根据需要设置 View 的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;
}

这假定标签应填满宽度并且图像应从左到右居中。

关于ios - 无法在 tableView 页脚中将 imageView 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com