[英]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)。
為了使表視圖按預期工作,您必須添加
UITableViewDataSource
和UITableViewDelegate
協議 UITableViewController
,至少實現必要的數據源方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.