zoukankan      html  css  js  c++  java
  • 自定义导航栏的返回按钮(xcode)

    导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行。

    正确的答案是重载UINavigationController类的pushViewController:animated方法。

    01 #import <UIKit/UIKit.h>
    02  
    03 <a href="http://my.oschina.net/interface" class="referer"target="_blank">@interface</a> MyNavigationController: UINavigationController {
    04  
    05 }
    06  
    07 <a href="http://my.oschina.net/end" class="referer" target="_blank">@end</a>
    08   
    09  
    10 #import "MyNavigationController.h"
    11  
    12 @implementation MyNavigationController
    13  
    14 -(void)popself
    15  
    16 {
    17  
    18     [self popViewControllerAnimated:YES];
    19  
    20 }
    21  
    22 -(UIBarButtonItem*) createBackButton
    23  
    24
    25  
    26 return [[UIBarButtonItem alloc]
    27  
    28 initWithTitle:@"返回"
    29  
    30 style:UIBarButtonItemStyleBordered
    31  
    32 target:self
    33  
    34 action:@selector(popself)];
    35  
    36
    37  
    38 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 
    39  
    40
    41  
    42     [super pushViewController:viewControlleranimated:animated]; 
    43  
    44 if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) { 
    45  
    46         viewController.navigationItem.leftBarButtonItem =[self createBackButton];
    47  
    48     
    49  
    50
    51  
    52 @end

     

    使用MyNavigationController替换UINavigationController。或者直接创建一个UINavigationController的新类别——不过,这招太毒了。会影响到所有的导航控制器。做人还是留一线的好。

  • 相关阅读:
    Java多线程-ThreadLocal和InheritableThreadLocal的使用
    Java多线程-join的使用
    VsCode配置让 ts 文件自动编译为 js文件
    npm和yarn更改依赖包全局下载和缓存路径
    npm和yarn更改淘宝镜像
    Java多线程-管道流实现线程间通信
    Java多线程-生产者/消费者模式实现
    Java多线程-使用 wait / notify 实现线程间的通信
    Java多线程-volatile关键字
    Java多线程-synchronized(非this对象)
  • 原文地址:https://www.cnblogs.com/zhwl/p/2387285.html
Copyright © 2011-2022 走看看