在iOS开发过程中,难免会需要不停的去创建视图控制器并进行适配,但是如果每次都创建新的视图控制器,对于一些可以通用的设置就会很繁琐,然后我就自己弄了个自定义的视图控制器,功能很简单,只是在视图控制器里面定义了对导航栏按钮的设置,界面的一些设置,如果有疑问,可以直接回复,看到了会第一时间回答.
CustomerViewController.h
//页面进入方式
@property (nonatomic, assign) BOOL isPresent;
/**
左侧按钮
@param title 字样 - 长度为0显示图片
@param action 方法
*/
- (void)setLeftButton:(NSString *_Nullable)title withSelector:(nullable SEL)action;
/**
右侧按钮
@param title 字样
@param action 方法
*/
- (void)setRightButton:(NSString *_Nullable)title withSelector:(nullable SEL)action;
/**
返回上一页面
*/
- (void)popBeforeControllerBack;
/**
返回根视图
*/
- (void)popRootViewControllerBack;
CustomerViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//状态栏不透明(必须设置,并且为NO)
self.navigationController.navigationBar.translucent = NO;
//意思就是延伸到边界
self.extendedLayoutIncludesOpaqueBars = YES;
//导航栏背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
//页面进入方式 yes代表presentview进入
// NSLog(@"页面进入方式 == %@",self.isPresent ? @"Present" : @"Push");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//返回上一页面
- (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];
}
}
//左侧按钮
- (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;//这个数值可以根据情况自由变化
self.navigationItem.leftBarButtonItems = @[leftBarButon,negativeSpacer];
}
//右侧按钮
- (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;//这个数值可以根据情况自由变化
self.navigationItem.rightBarButtonItems = @[rightBarButon,negativeSpacer];
}
实际调用时,直接在视图控制器中使用self就可以调用这些方法,毕竟已经在.h文件里声明过.
调用导航栏左侧按钮
//左边按钮
[self setLeftButton:@"" withSelector:@selector(placeBack)];
返回根视图
self popRootViewControllerBack];
isPresent 这个是因为有部分视图我是通过present方式推出来的,因此退出的时候需要用dismiss而不是pop方法,所以在进入视图之前,需设置该参数,因为不设置的情况下,该参数默认为NO,因此页面返回方式默认为pop方式.