iphone - 如何设置 UITableView 标题 View 的宽度?

标签 iphone ios ipad uitableview

有没有办法将 UITableView 部分标题 View 的宽度设置为小于 UITableView 的全宽?现在无论我设置 tableView:viewForHeaderInSection: 返回的 View 有多宽,标题 View 都会拉伸(stretch)到 UITableView 的宽度。

这是我的代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)];
  label.backgroundColor = [UIColor yellowColor];
  label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  return label;
}

最佳答案

试试这个。,如果我理解你的问题,这肯定对你有用

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
    headerView.backgroundColor = [UIColor clearColor];

    UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0,0, 50, 50)];
    label.backgroundColor = [UIColor yellowColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

    [headerView addSubview:label];
    return headerView;
}

关于iphone - 如何设置 UITableView 标题 View 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745180/

相关文章:

iphone - 如何找到准确的手机信号强度和服务提供商名称?

iphone - 计算 iPhone 上的像素大小

iPhone Facebook 应用 : Where does the "permissions array" code go?

ios - 使用 iOS 11 约束导航栏中的 UIBarButtonItem 大小

objective-c - 一个架构师如何在 Core Data 中创建具有通用关系的实体?

ios - XCode 4.5.1,应用程序窗口预计在应用程序启动结束时有一个 Root View Controller

iPhone串口错误

ios - 单击表格 View 单元格时调用

iphone - 我可以分发 iOS 开源代码以绕过应用商店吗?

iphone - 如何在隐藏控件时仅捕获 MPMoviePlayerController View 上的单击/点击?