繁体   English   中英

如何在顶部viewForHeaderInSection上添加UIButton

[英]How to add a UIButton on top viewForHeaderInSection

如何在viewForHeaderInSection顶部添加一个按钮。 我喜欢在节标题上方添加一个按钮,可以使用tableview滚动。 知道怎么做吗?

我在拉动刷新实现中看到的 - 相当hackish - 解决方案是简单地将“额外”视图作为子视图添加到表视图中 - 只需确保使用负y偏移来定位它。 该偏移量应等于视图高度(相当于-1倍)。 码:

UIView *myViewAboveHeader = // however you create it
CGRect f = myViewAboveHeader.frame;
f.origin.y = -1 * f.size.height;
myViewAboveHeader.frame = f;
[tableView addSubview:myViewAboveHeader];

编辑:似乎你不希望标题视图和它上面的视图。 在这种情况下,只需在表视图委托方法中返回的视图顶部添加一个按钮:

- (UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)s
{
    UIView *header = ...;
    UIButton *btn = // create a button somehow;
    [header addSubview:btn];
    return header;
}

使用此代码,将其放在viewdidload

- (void)viewDidLoad {
    UIView *headerViews = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];

    UIButton *managePrivacyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [managePrivacyButton setFrame:CGRectMake(0, 45, 320, 45)];
    managePrivacyButton.titleLabel.textAlignment = UITextAlignmentLeft;
    [managePrivacyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [managePrivacyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [headerViews addSubview:managePrivacyButton];
    [self.tableView setTableHeaderView:headerViews];
    [super viewDidLoad];
}

该方法特别将UIView作为返回。 UIButton是UIView的子类。 只需创建一个新的UIButton并将其返回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM