zoukankan      html  css  js  c++  java
  • 在UINavigationBar中添加左箭头返回按钮

    在iPhone里面最讨厌的控件之一就是UINavigationBar了。这个控件样式修改不方便,连添加按钮也特别麻烦。下面的例子是如何手动添加带箭头的按钮:

    UINavigationItem *item = [navBar.items objectAtIndex:0];
    UINavigationItem *back = [[UINavigationItem alloc] initWithTitle:@"Back"];
    NSArray *items = [[NSArray alloc] initWithObjects:back,item,nil];
    [navBar setItems:items];

    - (BOOL)navigationBar:(UINavigationBar *)navigationBar
            shouldPopItem:(UINavigationItem *)item{
        //在此处添加点击back按钮之后的操作代码  
        return FALSE;

    }

    Yes! There is a way of using the backBarButton with a custom action. No, it’s not by overriding the backBarButton property of navigationItem. This is not an ugly solution with images that simulate the “Back”-button (arrow-shaped.) It’s possible to use the backBarButton for popping the current viewController as normal, but than with other animations such as UIViewAnimationTransitionCurlDown.

    Enough said, the solution is simple. You have to subclass your navigationController’s  popViewControllerAnimated:(BOOL)animated. So create a custom navigationController:

    customNavigationController.h

    #import
    @interface customNavigationController : UINavigationController {}
    @end

    And a custom “popViewControllerAnimated:(BOOL)animated”, this popViewControllerAnimated-function uses the “UIViewAnimationTransitionCurlDown” when popping from a SettingsTableView.

    customNavigationController.m

    #import "customNavigationController.h"
    #import "SettingsTableController.h"
     
    @implementation customNavigationController
     
    - (UIViewController *)popViewControllerAnimated:(BOOL)animated
    {
     if([[self.viewControllers lastObject] class] == [SettingsTableController class]){
     
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration: 1.00];
      [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
         forView:self.view cache:NO];
     
      UIViewController *viewController = [super popViewControllerAnimated:NO];
     
      [UIView commitAnimations];
     
      return viewController;
     } else {
      return [super popViewControllerAnimated:animated];
     }
    }
    @end

    Use your custom navigationController in your appDelegate:

    customNavigationController *navigationController =
     [[customNavigationController alloc] 
      initWithRootViewController:rootView];

    14 Responses to “Custom action on Back Button UINavigationController”

  • 相关阅读:
    从零开始设计一套指令集及其虚拟机
    一种高效整数开平方算法:逐比特确认法
    C 可变参数函数的本质
    C 基础数据类型 性能测试
    用C在GBA上写光线追踪(0)配置开发编译环境
    用C#写小工具:将圆柱面贴图映射到半球贴图
    Linux 硬盘UUID相同处理方法
    ELK6.x_Kafka 安装配置文档
    Nagios4.x安装配置总结
    Cacti-0.8.8b详细安装及配置步骤
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175618.html
Copyright © 2011-2022 走看看