在表格底部添加带按钮的工具栏的最佳方法是什么?

时间:2014-10-08 21:48:24

标签: ios objective-c xcode

添加不滚动到表格底部的工具栏的最佳方法是什么?

我有一个可选择的名单表。 我希望在屏幕的底部有一个ToolBar(不一定是UIToolBar),用户可以按下按钮。我希望工具栏始终存在,不要随表滚动,始终位于底部。

1 个答案:

答案 0 :(得分:0)

以下是此问题的解决方案。

+(void)showToolBar:(UITableView *)tableViewObj
{
    UIView *parentView = tableViewObj.superview;

    CGRect frame, remain;
    CGRectDivide(parentView.bounds, &frame, &remain, 44, CGRectMaxYEdge);
    frame.origin.y = frame.origin.y+5.;
    frame.size.height = frame.size.height-12.;
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];

    [[UIToolbar appearance] setBackgroundColor:[UIColor colorWithRed:(245/255.0) green:(130/255.0) blue:(32/255.0) alpha:1.0]];
    toolbar.barTintColor = [UIColor colorWithRed:(245/255.0) green:(130/255.0) blue:(32/255.0) alpha:1.0];
    toolbar.translucent=NO;
    [toolbar setTintColor:[UIColor whiteColor]];

    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Sort" style:UIBarButtonItemStylePlain target:self action:nil];
   // [[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(10, -5) forBarMetrics:UIBarMetricsDefault];
    UIBarButtonItem *spacer  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc]initWithTitle:@"Categories" style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem *button3 = [[UIBarButtonItem alloc]initWithTitle:@"Filter" style:UIBarButtonItemStylePlain target:self action:nil];

    [toolbar setItems:[[NSArray alloc] initWithObjects:spacer, button1,spacer,button2,spacer,button3, spacer,nil]];

    [toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
    [tableViewObj.superview addSubview:toolbar];

     //ADD HORIZONTAL LINE BETWEEN BUTTONS
    float x = toolbar.frame.size.width / 3;
     //BUTTON1
    UIView *b1View = [[UIView alloc] initWithFrame:CGRectMake(x-1, 0, 1, 44)];
    b1View.backgroundColor= [UIColor whiteColor];
    [toolbar addSubview:b1View];
    //BUTTON2
    UIView *b2View = [[UIView alloc] initWithFrame:CGRectMake(x*2, 0, 1, 44)];
    b2View.backgroundColor= [UIColor whiteColor];
    [toolbar addSubview:b2View];
}