My VC is a subclass of UITableViewController
and I'm trying to add a UIButton
at the bottom of the screen.
I know how to do this when my VC is a UIView
with a UITableView
object in it, but how can I add this button when I'm subclassing UITableView
? I tried adding it in IB but it only lets me add it as the table's footer, which is only visible when the bottom of the table is scrolled to. Any ideas?
And also, how can I add an image behind the table while keeping the cells white? It's ea开发者_StackOverflow社区sy when the class is UIView
, but not sure in this case.
Suggestion1 : You could have create a separate view which contains your UIButton and place below of the UITableView.
Remark : This is useful because when you scroll the tableView the default footer will be stick to bottom.
// position(change according to your position) of the bottom view
//and set the size of the button
UIView* bottomView = [[UIView alloc] initWithFrame:CGRectMake(5.0, 460.0, 300.0, 80.0)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// position(change according to your position) of the button inside bottomView
//and set the size of the button
myButton.frame = CGRectMake(20, 20, 200, 44);
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[bottomView addSubview:myButton]; // add the button to bottom view
[self.view addSubView:bottomView]; //add bottom view main view
Suggestion2 : you can use viewForFooterInSection delegate method. where you can create a view and add the UILabel. viewForFooterInSection returns the UIView, which you can return your view which contains the UILabel
Remark : when you scroll the tableView the default footer will move along with your tableView
NOTE :
If your view is the subclass of UITableViewController, okay then change it to UIViewController and in your nib file Create a view and drag your tableView under the view and change the reference class to your UIViewController. Now create an IBOutlet of UITableView* myTableView and connect it to you nib file. you just need to change in your VC file for example self to [self.myTableView reloadData];
good code for this
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 80)] autorelease];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10,10, 300, 40);
[btn setTitle:@"showProfile" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(your_function) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:btn];
tbl.tableFooterView = headerView;
The problem with these approaches is that the UIButton
will not remain at the bottom of the screen for different device sizes since you're setting a fixed height for the footer and such footer is fixed to the table's bottom (which extends only to the last cell's bottom).
What I ended up doing was to get a handle of the main window's view and fix the button to the bottom of such.
I can provide code if someone would find it helpful.
add UIView
bellow in tableView
. subview of view is button and direct create IBAction
in your ViewController. i am using it
To have a view above a UITableView
inside a UITableViewController
fixed to bottom of the screen (with Auto-Layout):
Swift 4
// add inset to bottom of the table view, bottom = view height
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
// create the view
let myView = UIView()
myView.backgroundColor = .red
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
myView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
myView.bottomAnchor.constraint(equalTo: myView.bottomAnchor).isActive = true
Note that you have to insert this code in viewDidAppear()
in order to prevent the table view to have a greater zIndex than myView
.
精彩评论