iOS - 自定義視圖控制器

在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方式.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末纤控,一起剝皮案震驚了整個(gè)濱河市践美,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡僻族,警方通過查閱死者的電腦和手機(jī)粘驰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來述么,“玉大人蝌数,你說我怎么就攤上這事《让兀” “怎么了顶伞?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵饵撑,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我唆貌,道長(zhǎng)滑潘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任锨咙,我火速辦了婚禮语卤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘酪刀。我一直安慰自己粹舵,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布骂倘。 她就那樣靜靜地躺著眼滤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪历涝。 梳的紋絲不亂的頭發(fā)上诅需,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音睬关,去河邊找鬼诱担。 笑死,一個(gè)胖子當(dāng)著我的面吹牛电爹,可吹牛的內(nèi)容都是我干的蔫仙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼丐箩,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼摇邦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起屎勘,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤施籍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后概漱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丑慎,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年瓤摧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了竿裂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡照弥,死狀恐怖腻异,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情这揣,我是刑警寧澤悔常,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布影斑,位于F島的核電站,受9級(jí)特大地震影響机打,放射性物質(zhì)發(fā)生泄漏矫户。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一姐帚、第九天 我趴在偏房一處隱蔽的房頂上張望吏垮。 院中可真熱鬧,春花似錦罐旗、人聲如沸膳汪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)遗嗽。三九已至,卻和暖如春鼓蜒,著一層夾襖步出監(jiān)牢的瞬間痹换,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工都弹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娇豫,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓畅厢,卻偏偏與公主長(zhǎng)得像冯痢,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子框杜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容