iOS如何处理UITableView组头视图的旋转动画?

2026-05-27 22:371阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计391个文字,预计阅读时间需要2分钟。

iOS如何处理UITableView组头视图的旋转动画?

0x00+需求+组头视图包含一个UILabel+视图是靠右显示文字+当设备旋转后+文字居中显示了+因为UILabel的宽度写死了Q_Q+-+(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection section:


0x00 需求

组头视图内有一个​​UILabel​​​视图
是靠右显示文字
当设备旋转后
文字​​​居中​​显示了

因为​​UILabel​​的宽度写死了Q_Q

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *ID = @"header";
UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (!view) {
view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:ID];
[view.contentView addSubview:({
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30);
label.text = @"(0)";
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:17];
label.textAlignment = NSTextAlignmentRight;
label.tag = 100;
label;
})];
}

UILabel *label = [view.contentView viewWithTag:100];
label.text = [NSString stringWithFormat:@"(%@)",@(self.dataArray.count)];

return view;
}


0x01 autoresizingMask

设置了 ​​label​​​ 的 ​​autoresizingMask​​​ 后:
​​​label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;​​

​​label​​ 直接跑到屏幕外面去了(屏幕右边)Q_Q

原来​​label​​​的父视图​​contentView​​​一开始还没宽度
等到系统设置宽度后
​​​label​​就被弹到右边去了


0x01 取个巧

把​​label​​​的​​frame​​​设置修改一下,

​​​label.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30);​​​ 改为
​​​label.frame = CGRectMake(-200, 0, 200, 30);​​

配合
​​​label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;​​​ 搞定!
^ _ ^

如果还想设置一下右侧的边距
修改一下​​​x​​​即可
​​​label.frame = CGRectMake(-210, 0, 200, 30);​​

iOS如何处理UITableView组头视图的旋转动画?

本文共计391个文字,预计阅读时间需要2分钟。

iOS如何处理UITableView组头视图的旋转动画?

0x00+需求+组头视图包含一个UILabel+视图是靠右显示文字+当设备旋转后+文字居中显示了+因为UILabel的宽度写死了Q_Q+-+(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection section:


0x00 需求

组头视图内有一个​​UILabel​​​视图
是靠右显示文字
当设备旋转后
文字​​​居中​​显示了

因为​​UILabel​​的宽度写死了Q_Q

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *ID = @"header";
UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (!view) {
view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:ID];
[view.contentView addSubview:({
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30);
label.text = @"(0)";
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:17];
label.textAlignment = NSTextAlignmentRight;
label.tag = 100;
label;
})];
}

UILabel *label = [view.contentView viewWithTag:100];
label.text = [NSString stringWithFormat:@"(%@)",@(self.dataArray.count)];

return view;
}


0x01 autoresizingMask

设置了 ​​label​​​ 的 ​​autoresizingMask​​​ 后:
​​​label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;​​

​​label​​ 直接跑到屏幕外面去了(屏幕右边)Q_Q

原来​​label​​​的父视图​​contentView​​​一开始还没宽度
等到系统设置宽度后
​​​label​​就被弹到右边去了


0x01 取个巧

把​​label​​​的​​frame​​​设置修改一下,

​​​label.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 30);​​​ 改为
​​​label.frame = CGRectMake(-200, 0, 200, 30);​​

配合
​​​label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;​​​ 搞定!
^ _ ^

如果还想设置一下右侧的边距
修改一下​​​x​​​即可
​​​label.frame = CGRectMake(-210, 0, 200, 30);​​

iOS如何处理UITableView组头视图的旋转动画?