How to add a button such as the status bar of the messenger messenger

How can I add a header like this in my application and get a touch event to also control it to hide / show?

enter image description here

+4
source share
1 answer

Here's the work to do.

I made a new project for iOS 7 with one ViewController. Here is the storyboard:

Storyboard

I added a simple one UIViewat the top, UINavigationBarright below it and a large UIViewone that takes up the remaining space. This is the last view where you should post your content.

Now here is the ViewController code:

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 UIViewController UINavigationController. , iOS 6/7.

gif:

enter image description here

+3

Source: https://habr.com/ru/post/1532471/


All Articles