代码改变世界

iOS 隐藏系统的导航,使用自定义的导航

2016-02-23 15:46  甘雨路  阅读(641)  评论(0编辑  收藏  举报
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
    //隐藏系统导航栏
    navigation.navigationBarHidden = YES;
    self.window.rootViewController = navigation;
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "LFNavigationView.h"
#import "ViewController.h"
@interface RootViewController ()<LFNavigationViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
}
@property(nonatomic, strong) LFNavigationView *navigationView;

@end

@implementation RootViewController

- (void)loadView{
    [super loadView];
    _tableView = [[UITableView alloc] initWithFrame:/*[[UIScreen mainScreen] bounds]*/CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if([UIDevice currentDevice].systemVersion.floatValue >= 7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
    }
    [self setNavMethod];
}

- (void)setNavMethod{
    _navigationView = [[LFNavigationView alloc] initWithTitle:@"测试" WithLeftImage:nil WithLeftImageHighlighted:nil WithRightImage:nil WithRightImageHighlighted:nil WithViewControl:self];
    [self.view addSubview:_navigationView];
}

#pragma mark -- UITableViewDelegate --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"test row:%lu",indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ViewController *viewController = [[ViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@protocol  LFNavigationViewDelegate <NSObject>

@optional
- (void)leftButtonAction:(UIButton *)button;//左边按钮点击
- (void)rightButtonAction;//右边按钮点击

@end

@interface LFNavigationView : UIView

@property(nonatomic, weak) UIButton *leftBtn;
@property(nonatomic, weak) UIButton *rigthBtn;
@property(nonatomic, strong) UIImageView *rigthImg;
@property(nonatomic, strong) UILabel *rigthLabel;
@property(nonatomic, strong) UILabel *titleLabel;

- (id)initWithTitle:(NSString*)title WithLeftImage:(NSString*)leftImage WithLeftImageHighlighted:(NSString*)leftImageHighlighted WithRightImage:(NSString*)rightImage WithRightImageHighlighted:(NSString*)rigthImagehighlighted WithViewControl:(id<LFNavigationViewDelegate>) delegate;

@end
#import "LFNavigationView.h"

@implementation LFNavigationView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (id)initWithTitle:(NSString*)title WithLeftImage:(NSString*)leftImage WithLeftImageHighlighted:(NSString*)leftImageHighlighted WithRightImage:(NSString*)rightImage WithRightImageHighlighted:(NSString*)rigthImagehighlighted WithViewControl:(id<LFNavigationViewDelegate>) delegate{
    float Y = 0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 7) {
        Y = 20;
    }
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44+Y)];
    if (self) {
        UIView *bgView = [[UIView alloc] initWithFrame:self.frame];
        bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"顶栏背景"]];
        [self addSubview:bgView];
        
        if (title) {
            //标题
            _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 255)/2, Y, 255, 42)];
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.backgroundColor = [UIColor clearColor];
            _titleLabel.text = title;
            _titleLabel.textColor = [UIColor whiteColor];
            _titleLabel.font = [UIFont systemFontOfSize:20];
            [self addSubview:_titleLabel];
        }
        
        if (leftImage) {
            UIImageView *leftImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",leftImage]] highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",leftImageHighlighted]]];
            leftImg.frame  =CGRectMake(15, 32, 9, 16);
            [self addSubview:leftImg];
            UIImageView *leftTapView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 6+Y, 50, 30)];
            leftTapView.userInteractionEnabled = YES;
            leftTapView.backgroundColor = [UIColor clearColor];
            [self addSubview:leftTapView];
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:delegate action:@selector(leftButtonAction:)];
            tapGesture.numberOfTouchesRequired = 1;
            tapGesture.numberOfTapsRequired = 1;
            [leftTapView addGestureRecognizer:tapGesture];
        }
    }
    
    if (rightImage) {
        _rigthImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",rightImage]] highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",rigthImagehighlighted]]];
        _rigthImg.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-30, 35, 16, 16);
        [self addSubview:_rigthImg];
        
        _rigthLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 45, 35, 30, 16)];
        _rigthLabel.textColor = [UIColor whiteColor];
        _rigthLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_rigthLabel];
        
        UIImageView *rightTapView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 55, 6+Y, 50, 30)];
        rightTapView.userInteractionEnabled = YES;
        rightTapView.backgroundColor = [UIColor clearColor];
        [self addSubview:rightTapView];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:delegate action:@selector(rightButtonAction)];
        tapGesture.numberOfTouchesRequired = 1;
        tapGesture.numberOfTapsRequired = 1;
        [rightTapView addGestureRecognizer:tapGesture];
    }
    
    return self;
}

@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
#import "ViewController.h"
#import "LFNavigationView.h"
@interface ViewController ()<LFNavigationViewDelegate>

@property(nonatomic, strong) LFNavigationView *navigationView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    [self setNavMethod];
}

- (void)setNavMethod{
    _navigationView = [[LFNavigationView alloc] initWithTitle:@"控制器" WithLeftImage:@"返回按钮" WithLeftImageHighlighted:@"返回按钮_down" WithRightImage:@"加号图标" WithRightImageHighlighted:@"加号图标_down" WithViewControl:self];
    [self.view addSubview:_navigationView];
}

#pragma mark -- LFNavigationViewDelegate -- 
- (void)leftButtonAction:(UIButton *)button{
    NSLog(@"%@",button);
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)rightButtonAction{
    NSLog(@"should do somthing");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end