Search code examples
iosobjective-cuitableviewuibuttontablefooterview

How to fix a button at the bottom of the tableview?


In my App i want to fix a button at the bottom of the table view.

Here is my Initial Screen,

enter image description here

Button created at the footer section

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(tableView == educateTbl) 
{
    float footerWidth = 150.0f;
    float padding = 10.0f;
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
    addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
    [addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
    [addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addEdu.frame=CGRectMake(0, 0, 200, 30); 
    [footerView addSubview:addEdu];
    return footerView;
}
return nil;
}

After that i am getting like this,

enter image description here

How can i fix that?


Solution

  • why you dont make something like this?

    -(void)setTableFooter
    {
       float footerWidth = 150.0f;
       float padding = 10.0f;
       UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
       footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
       UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
       addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
       addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
       [addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
       [addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
       [addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addEdu.frame=CGRectMake(0, 0, 200, 30); 
       [footerView addSubview:addEdu];
    
       tableView.tableFooterView = footerView;
    
    }
    

    and call this after you init the table