ios - UITableView : set the header frame of view in UITableView doesn't work

标签 ios objective-c iphone uitableview

我需要在 UITableView 的第一节标题中添加一个 UIButton

这是我的代码:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
      if (section == 0) {

        UIButton *createGroupBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, tableView.frame.size.width - 100, 50)];
   // other code...
       return createGroupBtn;
     }
    return nil;
}

你看,我已经为页眉的框架添加了约束。 (例如,宽度小于 tableview 的框架的 100。)

以下是我 iPhone 中的结果:

enter image description here

显然,红色按钮的宽度仍然等于 tableview 的宽度。

我是否遗漏了其他一些使框架约束起作用的重要代码?

最佳答案

首先,您没有添加任何约束,您只是设置了框架。节标题始终与表格宽度相同。如果您希望按钮更窄,那么您应该创建一个 UIView,将按钮添加到其中(任意大小),然后将 View 作为节标题返回。

关于ios - UITableView : set the header frame of view in UITableView doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038818/

相关文章:

ios - "Call to ' malloc ' has an allocation size of 0 bytes"大小传递时出错?

iphone - 如何在 iOS 上将图像旋转 90 度?

ios - 如何在没有混合图层的情况下获得具有渐变的图像?

ios - 在 mergeChangesFromContextDidSaveNotification 中同步上下文时核心数据崩溃 (EXC_BAD_ACCESS)

iphone - 以编程方式添加 PageControl

objective-c - 如何在 iPhone 上进行 Base64 编码

objective-c - 如何有效地从 1 到 50 中选择几个唯一的随机数,不包括 x?

objective-c - 使用 parekit 进行简单的标记解析

iphone - iPhone 上的键盘和起床状态

iphone - iOS 中的 LocationManager 在调用委托(delegate)之前重新分配