新浪微博-03 自定义导航栏控制器

来源:互联网 发布:淘宝卖家怎么加入天猫 编辑:程序博客网 时间:2024/04/29 12:59

为了方便我们使用导航栏控制器

在这里 我们重写UINavigationController 中的push 方法

我们可以拿到所有push 进来的控制器 让我们来操作

并且我们写的这种方法侵入性很低 降低了类与类之间的耦合性

方便我们的使用和团队开发 下面添加了UIView 的分类方法方便我们的使用

#import <UIKit/UIKit.h>@interface UIView (Extension)@property(nonatomic,assign)CGFloat x;@property(nonatomic,assign)CGFloat y;@property(nonatomic,assign)CGFloat width;@property(nonatomic,assign)CGFloat height;@property(nonatomic,assign)CGSize size;@property(nonatomic,assign)CGPoint origin;@end

#import "UIView+Extension.h"@implementation UIView (Extension)-(void)setX:(CGFloat)x{    CGRect frame = self.frame;    frame.origin.x = x;    self.frame = frame;}-(CGFloat)x{    return self.frame.origin.x;}-(void)setY:(CGFloat)y{    CGRect frame = self.frame;    frame.origin.y  = y;    self.frame = frame;}-(CGFloat)y{    return self.frame.origin.y;}-(void)setWidth:(CGFloat)width{    CGRect frame = self.frame;    frame.size.width = width;    self.frame = frame;}-(CGFloat)width{    return self.frame.size.width;}-(void)setHeight:(CGFloat)height{    CGRect frame = self.frame;    frame.size.height = height;    self.frame = frame;}-(CGFloat)height{    return self.frame.size.height;}-(void)setSize:(CGSize)size{    CGRect frame = self.frame;    frame.size = size;    self.frame = frame;}-(CGSize)size{    return self.frame.size;}-(void)setOrigin:(CGPoint)origin{    CGRect frame = self.frame;    self.origin = origin;    self.frame = frame;}-(CGPoint)origin{    return self.frame.origin;}@end


#import "QHNavigationController.h"@interface QHNavigationController ()@end@implementation QHNavigationController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/**继承UINavigationController  //拦截 基本上都是通过自定义类 重写系统的方法来实现拦截   *  重写这个方法的目的 能够拦截所有push 进来的控制器 * *  @return viewController 即将push 进来的控制器 *///自定义UINavigationController-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{    //这里需要进行判断    //比较特殊的控制器有可能不需要     //如果重写必须调用父类的push 方法    [super pushViewController:viewController animated:YES];    if (self.viewControllers.count > 1) {//这时push进来的控制器ViewController 不是第一个子控制器        //    //设置图片                     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;             //这时一个自定义的方法                     viewController.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;                     viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:moreBtn];            }           NSLog(@"%lu %@",(unsigned long)self.viewControllers.count,viewController);       } -(void)back {#warning 这里的self 不是self.navigationController     //self 本来就是个导航控制器 self.navigationController 本来就是空的 [self popViewControllerAnimated:YES];//[self.navigationController] 是空的     //注意这里的写法!!! }  -(void)more { [self popToRootViewControllerAnimated:YES]; } /*#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
原创粉丝点击