通过继承封装导航栏,使导航栏上面出现的按钮和图标均相同
来源:互联网 发布:淘宝人像摄影教程 编辑:程序博客网 时间:2024/04/30 05:51
<pre name="code" class="objc"><p>当我们做多个导航控制器 我们需要自定义返回按钮 每次都要写大量的代码 </p><p>既浪费了时间 也没有从根本上解决我们的问题 </p><p>我们需要的功能是实现页面间的返回 并且右上角的按钮点击之后 可以直接返回到主页减少代码</p><p>为了减少代码 我们可以选择继承 </p><p>这是一种解决方法 </p><p>但是不建议大家这样做 因为apple 为我们提供的控制器太多了 </p><p>不一定都是UIViewController ,有UITableViewController UICollectionVIewController 如果我们通过继承 那么当UITableViewController 改为继承UIVIewController 那就出大问题了 本来有tableView 单继承下来就会报错 </p><p>下面给出一个例子 用于 一般的 </p><p>#import "QHBaseViewController.h"</p>@interface QHBaseViewController ()@end@implementation QHBaseViewController- (void)viewDidLoad { [super viewDidLoad]; //设置图片 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal]; [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted]; //设置尺寸 backBtn.size = backBtn.currentBackgroundImage.size; //这时一个自定义的方法 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn]; UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [moreBtn addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside]; [moreBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_more"] forState:UIControlStateNormal]; [moreBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] forState:UIControlStateHighlighted]; moreBtn.size = moreBtn.currentBackgroundImage.size; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:moreBtn]; // Do any additional setup after loading the view.}-(void)back{ [self.navigationController popViewControllerAnimated:YES];}-(void)more{ [self.navigationController popToRootViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
0 0
- 通过继承封装导航栏,使导航栏上面出现的按钮和图标均相同
- 自定义封装导航栏左右按钮点击事件和特定风格的控制器供多个控制器继承—— 当很多导航页面有相似的导航条时
- iOS 导航栏按钮封装
- 导航栏和菜单栏的封装
- 模拟QQ上面的导航按钮
- UISearchBar和导航栏按钮
- 改变导航栏上面系统自己带的返回按钮及颜色
- 导航栏图标问题
- Android 底部导航栏中间凸起、动态配置替换底部导航栏Tab图标(按钮、标签)的实现方案
- Swift - 修改导航栏“返回”按钮文字,图标
- Swift - 修改导航栏“返回”按钮文字,图标
- Swift - 修改导航栏“返回”按钮文字,图标
- 导航栏自定义按钮
- 替换导航栏按钮
- 自定义导航栏按钮
- 导航栏添加按钮
- 导航栏按钮返回
- 底部导航栏封装
- Wiki - 解决 IOError: decoder jpeg not available
- String
- http协议的工作原理?
- C++数据结构之List--线性实现
- Unity Web Player修改加载页面的LOGO
- 通过继承封装导航栏,使导航栏上面出现的按钮和图标均相同
- 数据仓库之ETL应用
- grunt使用笔记
- hdu5433 Xiao Ming climbing(BestCoder Round #55 ($) )
- iOS正则表达式使用
- 签证之家
- php向数据库中插入中文乱码问题
- HDU 5439 Aggregated Counting
- 利用Aspose.Words打印word文档