iOS - 自定義視圖控制器

在iOS開發(fā)過程中,難免會需要不停的去創(chuàng)建視圖控制器并進行適配,但是如果每次都創(chuàng)建新的視圖控制器,對于一些可以通用的設(shè)置就會很繁瑣,然后我就自己弄了個自定義的視圖控制器,功能很簡單,只是在視圖控制器里面定義了對導航欄按鈕的設(shè)置,界面的一些設(shè)置,如果有疑問,可以直接回復,看到了會第一時間回答.


CustomerViewController.h

//頁面進入方式

@property (nonatomic, assign) BOOL isPresent;

/**

左側(cè)按鈕

@param title 字樣 - 長度為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;

/**

返回上一頁面

*/

- (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;

? ? //導航欄背景顏色

? ? 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];

? ? }


}

//左側(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;//這個數(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;//這個數(shù)值可以根據(jù)情況自由變化

? ? self.navigationItem.rightBarButtonItems = @[rightBarButon,negativeSpacer];


}

實際調(diào)用時,直接在視圖控制器中使用self就可以調(diào)用這些方法,畢竟已經(jīng)在.h文件里聲明過.

調(diào)用導航欄左側(cè)按鈕

//左邊按鈕
??? [self setLeftButton:@"" withSelector:@selector(placeBack)];

返回根視圖

self popRootViewControllerBack];

isPresent 這個是因為有部分視圖我是通過present方式推出來的,因此退出的時候需要用dismiss而不是pop方法,所以在進入視圖之前,需設(shè)置該參數(shù),因為不設(shè)置的情況下,該參數(shù)默認為NO,因此頁面返回方式默認為pop方式.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容