【问题标题】:What's the best way to add a view under UINavigationBar in iOS7在 iOS 7 的导航栏下添加视图的最佳方法是什么
【发布时间】:2014-01-28 14:16:03
【问题描述】:

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

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

【问题讨论】:

  • 为什么不定义自己的自定义 UIView?
  • 它看起来更像是一个简单的视图帧动画...
  • @MrBr 你的意思是而不是 UIToolBar?
  • 如果您使用 UIScrollView,您可以检测到该视图何时位于顶部并相应地显示/隐藏自定义视图(如果您愿意,可以在其中使用 UIToolBar)。
  • 看下面的简单方法。

标签: ios ios7 uinavigationbar uitoolbar


【解决方案1】:

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

  • 像这样添加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

希望对您有所帮助。

【讨论】:

猜你喜欢
  • 2015-01-08
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
相关资源
最近更新 更多