我正在开发一个应用程序,它需要标题的部分应该在右侧而不是默认的左侧。右侧的UITableView部分标题而不是默认的左侧
我搜索,很多爱好者建议实施:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static UIView *headerView;
if (headerView != nil)
return headerView;
NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);
// set the container width to a known value so that we can center a label in it
// it will get resized by the tableview since we set autoresizeflags
float headerWidth = 150.0f;
float padding = 10.0f; // an arbitrary amount to center the label in the container
headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// create the label centered in the container, then set the appropriate autoresize mask
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
headerLabel.textAlignment = UITextAlignmentRight;
headerLabel.text = headerText;
[headerView addSubview:headerLabel];
return headerView;
}
然而,当我尝试增加头视图帧的x坐标,它不会影响视图的位置。它始终位于桌子的中央。
我需要标题在右侧。
而是用flexibleWidth去的,你不能强迫它是全宽?然后视图将消耗完整的宽度并证明文本是正确的。 – bryanmac
您已经选择了一个大多数情况下正确的答案,但添加空格来获得保证金是一个真正的破解。解决你的问题的正确方法是创建一个空的视图(或一个背景颜色)并将其用作headerView。然后你添加一个UILabel到该容器视图,并从右侧偏移它。这个概念 - 使用空视图作为容器来放置其他视图是一种常用且有用的工具。 –
@DavidH你是对的。增加空格的想法并不好。虽然它暂时对我有效。正如我在我的问题中提到的那样,观点总是在中心位置,因为我需要它在右边。设置框架不起作用。此外,该标签是右对齐的。我明白使用UIView有更多的自定义选项。感谢您的评论! –