如何添加像facebook messenger状态栏这样的按钮

时间:2014-03-19 13:09:03

标签: ios iphone objective-c ios7

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

enter image description here

1 个答案:

答案 0 :(得分:3)

这是可以解决的问题。

我为iOS 7制作了一个新项目,只有一个ViewController。这是故事板:

Storyboard

我在顶部添加了一个简单的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实现的,我手动添加了导航栏。此代码无法在嵌入UITableViewController的{​​{1}}或UIViewController中使用。它不使用autolayout,你绝对应该在iOS 6/7下使用。

这是gif预览:

enter image description here