如何显示导航栏

时间:2014-04-12 15:25:18

标签: ios xcode gridview uiview navigationbar

如何编写一个 CODE ,在单击navigationBar右侧项目按钮隐藏后,在UIView的网格视图中显示navigationBar?我的设计是,当点击网格视图时,它将显示其大图。我的项目编写代码而不使用故事板。谢谢你的回复!

我希望我可以发布图像来描述它,并让它易于理解。 但是,我是新来的。

韩国社交协会!代码分为两部分......

//part-1 code:
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title=NSLocalizedString(@"showPhoto", nil);
    MainViewController *mainController=[self revealViewController];
    [self.navigationController.navigationBar addGestureRecognizer:mainController.panGestureRecognizer];

    //leftBarButtonItem
    UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:      [UIImage imageNamed:@"left.png"]                                                                         style:UIBarButtonItemStyleBordered target:mainController action:@selector(revealToggle:)];
    self.navigationItem.leftBarButtonItem = revealButtonItem;

    //rightButton ------
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"right.png"]                                                                               style:UIBarButtonItemStyleBordered target:self action:@selector(showHideNavbar:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

    -(void) showHideNavbar:(id) sender{
    if (self.navigationController.navigationBar.hidden == NO)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    else if (self.navigationController.navigationBar.hidden == YES)
    {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}


//part-2 code:

//tapGestureDetected:
    -(void)tapGestureDetected:(UITapGestureRecognizer *)gesture{
    //??????xxxxxxx
    if (self.navigationController.navigationBar.hidden == NO)
    {
        // hide the Navigation Bar
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    if (self.navigationController.navigationBar.hidden == YES)
    {
        // Show the Navigation Bar
        [self.navigationController setNavigationBarHidden:NO animated:YES];

    }//??????xxxxxxx

    if(_lastTouchedPosition.row >= 0 && _lastTouchedPosition.column >= 0)
    {
        [_easeThread cancel];
        _easeThread = nil;
        [UIView animateWithDuration:.2 animations:^
         {
             [self reloadData];
         } completion:^(BOOL finished)
         {
             _lastTouchedPosition = DWPositionMake(-55, -55);
         }];
    }

    if([self.delegate respondsToSelector:@selector(gridView:didSelectCell:atPosition:)])
    {
        DWPosition touchPosition = [self determinePositionAtPoint:[gesture locationInView:self]];

        if(touchPosition.row != _lastTouchedPosition.row && touchPosition.column != _lastTouchedPosition.column)
        {
            DWGridViewCell *cell = [self.delegate gridView:self cellAtPosition:touchPosition];
            [self.delegate gridView:self didSelectCell:cell atPosition:touchPosition];

        }
    }
}


//didSelectCell
-(void)gridView:(DWGridView *)gridView didSelectCell:(DWGridViewCell *)cell atPosition:(DWPosition)position
    {
    NSDictionary *cellDictionary = [self cellDictionaryAtPosition:position];
    UIImage *image = [cellDictionary objectForKey:@"Image"];

    UIButton *button = [[UIButton alloc] init];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchDown];

    UIViewController *contr = [[UIViewController alloc] init];
    [contr.view addSubview:button];

    [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];
    [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];

    [self presentViewController:contr animated:YES completion:nil];
}

1 个答案:

答案 0 :(得分:0)

您需要将视图控制器添加到导航控制器,然后以模态方式显示。要添加右侧导航按钮,请在视图控制器rightBarButtonItemnavigationItem上进行设置。要解雇它,您需要像UIButton一样向UIBarButtonItem添加目标。

由于您当前约束button以填充整个视图控制器,因此在添加导航栏时,这很可能无效 - 它将显示在导航栏下方。要解决这个问题,顶部垂直约束需要针对topGuide(在代码中称为topLayoutGuide,而在可视格式语言中称为topGuide而不是视图控制器{{1 }}

view

如果您还想在点击图片时关闭模态弹出窗口,请将另一个目标添加到指向//didSelectCell -(void)gridView:(DWGridView *)gridView didSelectCell:(DWGridViewCell *)cell atPosition:(DWPosition)position { NSDictionary *cellDictionary = [self cellDictionaryAtPosition:position]; UIImage *image = [cellDictionary objectForKey:@"Image"]; UIButton *button = [[UIButton alloc] init]; button.translatesAutoresizingMaskIntoConstraints = NO; [button setBackgroundImage:image forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchDown]; UIViewController *contr = [[UIViewController alloc] init]; contr.navigationItem.rigthBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(didTapToDismissBigPicture)]; [contr.view addSubview:button]; [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contr]; [self presentViewController:navController animated:YES completion:nil]; } // Button target -(void)didTapToDismissBigPicture { [self dismissViewControllerAnimated:YES completion:NULL]; } 的{​​{1}},我没有显示,因为您已经有一个目标按钮,所以我不知道你在做什么