[英]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.