在iOS7中在UINavigationBar下添加视图的最佳方法是什么

时间:2014-01-28 14:16:04

标签: ios ios7 uinavigationbar uitoolbar

在ios7上,许多应用程序(Apple Messages,Facebook Messenger,Calendar)都会在UINavigationBar下显示视图,通常会显示标准动画。由于看起来非常标准并且使用UIToolBar看起来很多,我一直在寻找实现它的标准方法但却找不到任何东西。

有没有更好的方法将UIToolBar添加到UINavigationBar?

Apple Messages

1 个答案:

答案 0 :(得分:5)

你应该遵循这个简单的方法。

  • 像这样添加UIToolBar

    UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    
    NSArray *items = [NSArray arrayWithObjects:item1, flexiableItem, item2, nil];
    self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, 44)];
    [self.toolBar setItems:items];
    self.toolBar.tintColor = [UIColor whiteColor];
    self.toolBar.barTintColor = [UIColor colorWithRed:0.6 green:0.1 blue:0.2 alpha:1];
    [self.contentView addSubview:self.toolBar];
    
  • 在顶部导航项

    上添加菜单按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMenu:)];
    
  • 现在实施toggleMenu功能。添加BOOL变量以跟踪移动。

    if(!moved) {
    [UIView animateWithDuration:0.5 animations:^{
        self.toolBar.alpha = 1;
        self.toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    }];
    moved = YES;
    }else {
    [UIView animateWithDuration:0.5 animations:^{
        self.toolBar.alpha = 1;
        self.toolBar.frame = CGRectMake(0, -44, self.view.frame.size.width, 44);
    }];
    moved = NO;
    }
    
  • 以下是附件video

希望有所帮助。

相关问题