簡體   English   中英

以編程方式將UIButton添加到UITableView中,但按鈕不顯示

[英]Add UIButton to UITableView programmatically but the button doesn't show

我想在表格視圖上方添加一個按鈕,而不是與表格視圖一起滾動。 但是該按鈕沒有顯示。 有任何想法嗎?

UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setImage:[UIImage imageNamed:@"refresh"] forState:UIControlStateNormal];
CGRect applicationFrame = [UIScreen mainScreen].bounds;
CGFloat screenWidth = CGRectGetWidth(applicationFrame);
CGFloat screenHeight = CGRectGetHeight(applicationFrame);
CGFloat buttonWidth = 40.0f;
CGFloat buttonHeight = 40.0f;
CGFloat spacing = 10.0f;
CGFloat buttonX = screenWidth - buttonWidth - spacing;
CGFloat buttonY = screenHeight - buttonHeight - spacing;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
refreshButton.frame = buttonFrame;
[refreshButton addTarget:self
                  action:@selector(clickRefreshButton)
        forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:refreshButton];

提前致謝!

您不能使用UITableViewController做到這一點。 原因:視圖控制器的view屬性表視圖,因此您不能在其上方添加任何內容。

做到這一點的方法是使用普通的UIViewController並將按鈕和UITableView添加為子視圖(在Interface Builder中比在代碼中更好,BTW)。

為了使表視圖按預期工作,您必須添加

  • 表格視圖的插座
  • 采用UITableViewDataSourceUITableViewDelegate協議
  • 與在UITableViewController ,至少實現必要的數據源方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM