簡體   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