如何添加类似Facebook Messenger状态栏的按钮

4
我该如何在我的应用程序中添加这样的标题,并接收触摸事件以控制它的隐藏/显示?

enter image description here


在您的UIViewController中添加一个简单的UIView,其框架大小为(0,0,320,40),并添加一个标签和UITapGestureRecognizer。在点击时执行您想要的任何操作。 - rdurand
你的答案在这个指南里:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html#//apple_ref/doc/uid/TP40011313-CH2-SW1 - Cy-4AH
世纪的简单秘密 ;) - deimus
1个回答

3

这里有一些需要处理的内容。

我创建了一个新的iOS 7项目,其中只有一个ViewController。以下是故事版:

故事版

我在顶部添加了一个简单的UIView,紧接着它下面是一个UINavigationBar,然后还有一个大的UIView,占据了剩余的空间。最后一个视图是您应该放置内容的地方。

现在,这是ViewController的代码:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *littleView;
@property (weak, nonatomic) IBOutlet UINavigationBar *navBar;
@property (weak, nonatomic) IBOutlet UIView *mainView;
- (IBAction)toggleLittleView:(id)sender;

@end

littleView 是状态栏后面的视图,navbar 是导航栏,mainView 是内容视图,toggleLittleview 与导航栏中的按钮相关联。

ViewController.m :

@implementation ViewController {

    BOOL isVisible;
    CGFloat statusBarOffset;
}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Setting initial values
    isVisible = YES;
    statusBarOffset = 20;

    // Adding a gesture recognizer to littleView
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [_littleView addGestureRecognizer:tap];
}

// Linked to the nav bar button
- (IBAction)toggleLittleView:(id)sender {

    // If littleView is not on screen, show it before animation
    if (!isVisible) {
        _littleView.hidden = !_littleView.hidden;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }

    // Animate to the new frames
    [UIView animateWithDuration:0.25
                     animations:^{

                         _littleView.frame = CGRectOffset(_littleView.frame, 0, isVisible ? -(_littleView.frame.size.height-statusBarOffset) : (_littleView.frame.size.height-statusBarOffset));
                         _navBar.frame = CGRectMake(_navBar.frame.origin.x, _littleView.frame.origin.y + _littleView.frame.size.height, _navBar.frame.size.width, _navBar.frame.size.height);
                         CGFloat offSet = isVisible ? self.view.frame.size.height - _navBar.frame.size.height + statusBarOffset : self.view.frame.size.height - _navBar.frame.size.height - _littleView.frame.size.height + statusBarOffset;
                         _mainView.frame = CGRectMake(_mainView.frame.origin.x, _navBar.frame.origin.y + _navBar.frame.size.height, _mainView.frame.size.width, offSet);

                         isVisible = !isVisible;
                     }
                     completion:^(BOOL finished) {

                         // If view is not visible after animation, hide it
                         if (!isVisible) {
                             _littleView.hidden = !_littleView.hidden;
                             [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault];
                         }
                     }];
}

// Do stuff on tap
-(void)handleTap:(UIGestureRecognizer*)tap {

    [[[UIAlertView alloc] initWithTitle:@"Test" message:@"You tapped !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

@end

警告

这段代码并不完美,但它基本上可以复制你想要的效果。你应该对其进行调整以使其行为符合你的期望。此外,这是在一个简单的UIViewController中实现的,而不是嵌入在一个UINavigationController中,并且我手动添加了导航栏。这段代码将无法在UITableViewControllerUIViewController嵌入在UINavigationController中时使用。它不使用自动布局,在iOS 6/7下,你绝对应该使用它。

这里是gif预览:

enter image description here


这个在导航控制器中不起作用,对吧? - Chris
你需要在代码上花一些功夫,但它应该是可适应的。 - rdurand
重点是,您需要一个UIToolBar才能使用Storyboard IB在状态栏和工具栏之间放置一个UIView。如果使用嵌入在UINavigationController中的View,您不能仅仅将一个视图放置在它们之间,或者我错了吗? - Chris
没错。但我相信只要花点功夫,这在代码中是可行的。 - rdurand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接