How To Add UIButton Below UITableVIew?

11,949

Solution 1

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];

Solution 2

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;
Share:
11,949
Jon
Author by

Jon

Updated on June 04, 2022

Comments

  • Jon
    Jon almost 2 years

    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 easy when the class is UIView, but not sure in this case.

  • Jon
    Jon over 12 years
    Its actually a UIButton, not a UILabel. Would this still work with option 1 you gave?
  • vikingosegundo
    vikingosegundo over 12 years
    tbl.tableFooterView = headerView; assigning a header as a footer — funny.
  • Jon
    Jon over 12 years
    Thanks, can you just add the code that lets me add the new uiview as a subivew and place it below the table that would go in viewdidload? Thanks.
  • Praveen-K
    Praveen-K over 12 years
    I have enhanced my answer and have written the code there itself. :)
  • Jon
    Jon over 12 years
    Thanks, for some reason, that code makes the button scroll with the footer of the tableview
  • Praveen-K
    Praveen-K over 12 years
    No. it would not, that is why we kept the tableView and button separately in the different section. you can scroll your tableView but your button(which is added to bottomView) will stick to bottom always.
  • Jon
    Jon over 12 years
    I copied you code directly into my viewDidLoad and I can only see the button when I scroll all the way down to the bottom of my table.
  • Praveen-K
    Praveen-K over 12 years
    How it can possible?, is your tableView overlap the bottom view(set the backgroundcolor of bottomView to some color and make sure tableView should not overlap the bottomView). the total height of UIView for iPhone is 480. then set the height for tableView 450 and for bottomView 30. Just change the frame position of tableView and bottomView to make sure that both should not overlap..
  • Jon
    Jon over 12 years
    OK, that may be the problem. How can I change the heights for both my uiview and the bottom uiview so they fit? I will then accept your answer. Thanks.
  • Praveen-K
    Praveen-K over 12 years
    why you want to change the frame size of uiview? you just need to change the frame size of your tableView and bottom view. okay i got it 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]; or else paste your code to pastebin.com
  • Jon
    Jon over 12 years
    Control-V, I have to have it as UITableView subclass not a UIView, because I couldn't get search working unless it as a talbe subclass.
  • Praveen-K
    Praveen-K over 12 years
    Hahaha funny, who told you can not search working if you do not subclass to UITableViewController. Ohh you only asked this question stackoverflow.com/questions/6501032/…. Read This( bit.ly/rujhui )
  • Jon
    Jon over 12 years
    Ok thanks, i got search working when switching to Uiview i think. Thanks.
  • Praveen-K
    Praveen-K over 12 years
    Feel free to ask anytime..
  • aVC
    aVC almost 11 years
    @vikingosegundo good when you hold the phone in inverted portrait mode ;)