在iOS開發(fā)過程中,難免會(huì)需要不停的去創(chuàng)建視圖控制器并進(jìn)行適配,但是如果每次都創(chuàng)建新的視圖控制器,對(duì)于一些可以通用的設(shè)置就會(huì)很繁瑣,然后我就自己弄了個(gè)自定義的視圖控制器,功能很簡(jiǎn)單,只是在視圖控制器里面定義了對(duì)導(dǎo)航欄按鈕的設(shè)置,界面的一些設(shè)置,如果有疑問,可以直接回復(fù),看到了會(huì)第一時(shí)間回答.
CustomerViewController.h
//頁(yè)面進(jìn)入方式
@property (nonatomic, assign) BOOL isPresent;
/**
左側(cè)按鈕
@param title 字樣 - 長(zhǎng)度為0顯示圖片
@param action 方法
*/
- (void)setLeftButton:(NSString *_Nullable)title withSelector:(nullable SEL)action;
/**
右側(cè)按鈕
@param title 字樣
@param action 方法
*/
- (void)setRightButton:(NSString *_Nullable)title withSelector:(nullable SEL)action;
/**
返回上一頁(yè)面
*/
- (void)popBeforeControllerBack;
/**
返回根視圖
*/
- (void)popRootViewControllerBack;
CustomerViewController.m
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? //狀態(tài)欄不透明(必須設(shè)置盖高,并且為NO)
? ? self.navigationController.navigationBar.translucent = NO;
? ? //意思就是延伸到邊界
? ? self.extendedLayoutIncludesOpaqueBars = YES;
? ? //導(dǎo)航欄背景顏色
? ? self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
? ? self.automaticallyAdjustsScrollViewInsets = NO;
? ? //頁(yè)面進(jìn)入方式 yes代表presentview進(jìn)入
//? ? NSLog(@"頁(yè)面進(jìn)入方式 == %@",self.isPresent ? @"Present" : @"Push");
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
//返回上一頁(yè)面
- (void)popBeforeControllerBack {
? ? if (self.isPresent) {
? ? ? ? [self dismissViewControllerAnimated:YES completion:nil];
? ? } else {
? ? ? ? [self.navigationController popViewControllerAnimated:YES];
? ? }
}
//返回根視圖
- (void)popRootViewControllerBack {
? ? if (self.isPresent) {
? ? ? ? [self dismissViewControllerAnimated:YES completion:nil];
? ? } else {
? ? ? ? [self.navigationController popToRootViewControllerAnimated:YES];
? ? }
}
//左側(cè)按鈕
- (void)setLeftButton:(NSString *_Nullable)title withSelector:(nullable SEL)action {
? ? UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
? ? if (title.length == 0) {
? ? ? ? [left setImage:[UIImage imageNamed:@"popBack"] forState:UIControlStateNormal];
? ? } else {
? ? ? ? CGFloat width = [title getWidthWithMaxHeight:30 WithFont:15];
? ? ? ? if (width < 40) {
? ? ? ? ? ? width = 40;
? ? ? ? }
? ? ? ? CGFloat height = self.navigationController.navigationBar.frame.size.height;
? ? ? ? left.frame = CGRectMake(0, 0, width, height);
? ? ? ? [left setTitle:title forState:UIControlStateNormal];
? ? ? ? left.titleLabel.font = FCFont(16);
? ? ? ? [left setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? }
? ? [left addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
? ? left.clipsToBounds = YES;
? ? UIBarButtonItem *leftBarButon = [[UIBarButtonItem alloc] initWithCustomView:left];
? ? UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
? ? negativeSpacer.width = 5;//這個(gè)數(shù)值可以根據(jù)情況自由變化
? ? self.navigationItem.leftBarButtonItems = @[leftBarButon,negativeSpacer];
}
//右側(cè)按鈕
- (void)setRightButton:(NSString *_Nullable)title withSelector:(nullable SEL)action {
? ? CGFloat width = [title getWidthWithMaxHeight:30 WithFont:15];
? ? if (width < 40) {
? ? ? ? width = 40;
? ? }
? ? CGFloat height = self.navigationController.navigationBar.frame.size.height;
? ? UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width * 3 / 2.0, height)];
? ? [right setTitle:title forState:UIControlStateNormal];
? ? [right setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? right.titleLabel.font = FCFont(16);
? ? [right addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
? ? right.clipsToBounds = YES;
? ? UIBarButtonItem *rightBarButon = [[UIBarButtonItem alloc] initWithCustomView:right];
? ? UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
? ? negativeSpacer.width = 5;//這個(gè)數(shù)值可以根據(jù)情況自由變化
? ? self.navigationItem.rightBarButtonItems = @[rightBarButon,negativeSpacer];
}
實(shí)際調(diào)用時(shí),直接在視圖控制器中使用self就可以調(diào)用這些方法,畢竟已經(jīng)在.h文件里聲明過.
調(diào)用導(dǎo)航欄左側(cè)按鈕
//左邊按鈕
??? [self setLeftButton:@"" withSelector:@selector(placeBack)];
返回根視圖
self popRootViewControllerBack];
isPresent 這個(gè)是因?yàn)橛胁糠忠晥D我是通過present方式推出來的,因此退出的時(shí)候需要用dismiss而不是pop方法,所以在進(jìn)入視圖之前,需設(shè)置該參數(shù),因?yàn)椴辉O(shè)置的情況下,該參數(shù)默認(rèn)為NO,因此頁(yè)面返回方式默認(rèn)為pop方式.