从导航栏下拉菜单

时间:2015-01-07 00:30:38

标签: ios objective-c menu uinavigationbar custom-controls

有没有人知道将可伸缩菜单附加到导航栏底部的好方法?

我发现了这个:https://github.com/mentionapp/mntpulltoreact,但是这样做的行为是拉下整个视图,不放手,然后通过拖动手指选择想要的项目。

Zappos应用程序有一个完美的例子,我正在寻找。有谁知道他们可以使用什么或现有的解决方案?我搜索过cocoacontrols并没有找到太多。 enter image description here

2 个答案:

答案 0 :(得分:1)

做这样的事情对你有用吗?那么你当然可以在菜单视图中添加你需要的任何菜单按钮。

UIView * menu;
UIButton * activate;
int menuHeight;
int buttonHeight;

-(void)toggleMenu:(UIButton*)sender{

    [sender setUserInteractionEnabled:NO];

    [self.view bringSubviewToFront:menu];

    if (sender.tag == 1000){

        [UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y - menu.frame.size.height + buttonHeight ,menu.frame.size.width,menu.frame.size.height);

            menu.frame = frame;

            [sender setUserInteractionEnabled:YES];

        } completion:nil];

        sender.tag = 2000;

    } else {

        [UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y + menu.frame.size.height - buttonHeight  ,menu.frame.size.width,menu.frame.size.height);

            menu.frame = frame;

            [sender setUserInteractionEnabled:YES];

        } completion:nil];

        sender.tag = 1000;
    }

}



menuHeight = 100;
buttonHeight = 20;

activate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[activate addTarget:self action:@selector(toggleMenu:) forControlEvents:UIControlEventTouchUpInside];
[activate setTitle:@"Menu" forState:UIControlStateNormal];
activate.frame = CGRectMake(80.0, menuHeight - buttonHeight, 70.0, buttonHeight);
activate.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;

activate.tag = 2000;

menu = [[UIView alloc] initWithFrame:CGRectMake(0,0 - menuHeight + buttonHeight,self.view.bounds.size.width, menuHeight)];

menu.autoresizingMask =  UIViewAutoresizingFlexibleWidth;

menu.backgroundColor = [UIColor grayColor];

[self.view addSubview:menu];
[menu addSubview:activate];

答案 1 :(得分:1)

然后,您可以使用一些约束来将表格向下移动,而不是。这是未经测试的,但应该让你指向正确的方向

-(void)toggleMenu:(UIButton*)sender{


    UIView * menu;

    [sender setUserInteractionEnabled:NO];

    [self.view bringSubviewToFront:menu];

    id topGuide = self.topLayoutGuide;
    id bottomGuide = self.bottomLayoutGuide;

    NSDictionary * viewsDictionary;

    viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);

    NSArray * newVertConstraint;


    if (sender.tag == 1000){

        newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", 0.0] options:0 metrics: 0 views:viewsDictionary];

        sender.tag = 2000;

    } else {
        newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", menu.frame.size.height] options:0 metrics: 0 views:viewsDictionary];        
        sender.tag = 1000;
    }

    [self.view removeConstraints:vertConstraint];

    [self.view addConstraints:newVertConstraint];
    [self.view setNeedsUpdateConstraints];
    [UIView animateWithDuration:1.5 animations:^{
        [self.view layoutIfNeeded];
    }];

    vertConstraint = newVertConstraint;

}




NSArray * vertConstraint;

UITableView * myTable = [[UITableView alloc] init];
[self.view addSubview:myTable];


self.automaticallyAdjustsScrollViewInsets = NO;
[myTable setTranslatesAutoresizingMaskIntoConstraints: NO];
[menu setTranslatesAutoresizingMaskIntoConstraints: NO];

id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;

NSDictionary * viewsDictionary;

viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);

vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[bottomGuide][menu(==0)][myTable][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary];

[self.view addConstraints:vertConstraint];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[menu]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myTable]|" options:0 metrics: 0 views:viewsDictionary]];