在iOS7中在UINavigationBar下添加视图的最佳方法是什么 [英] What's the best way to add a view under UINavigationBar in iOS7

查看:145
本文介绍了在iOS7中在UINavigationBar下添加视图的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On ios7, a lot of apps (Apple Messages, Facebook Messenger, Calendar)have views appearing under the UINavigationBar, often with what seems to be standard animation. As it seems quite standard and looks a lot with a UIToolBar, I was looking for the standard way of implementing it but couldn't find anything.

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

Is there a better way to adding a UIToolBar to the UINavigationBar?

推荐答案

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

You should follow this simple approach.


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


  • 在顶部导航项目上添加菜单按钮

  • Add a Menu Button on Top navigation item

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMenu:)];
    


  • 现在实施 toggleMenu 功能。添加 BOOL 变量来跟踪移动。

  • Now Implement toggleMenu function. Add a BOOL variable to track the movement.

    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;
    }
    


  • 这是附件视频

    希望有所帮助。

    这篇关于在iOS7中在UINavigationBar下添加视图的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆