What is the best way to add a view under UINavigationBar in iOS7

On ios7, many applications (Apple Messages, Facebook Messenger, Calendar) have views displayed under the UINavigationBar, often with what seems like standard animations. Since this seems quite standard and looks a lot like UIToolBar, I was looking for a standard way to implement it, but could not find anything.

Is there a better way to add a UIToolBar to a UINavigationBar?

Apple messages

+4
source share
1 answer

You must follow this simple approach.

  • Add UIToolBarlike that.

    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 at the top of the navigation item

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

, .

+5

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


All Articles