iOS開發(fā)-基礎(chǔ)框架
本基礎(chǔ)框架主要包括UITabBarController,UINavigationController和UIBarButtonItem類的擴(kuò)展。主要解決子視圖創(chuàng)建過多,封裝帶有UINavigationController的子視圖創(chuàng)建,然后添加到UITabBarController的ChildViewController視圖中。針對UITabBarController的UITabBarItem的字體大小,顏色設(shè)置。廢話不多說,上代碼。
一.繼承UITabBarController創(chuàng)建的NPTabBarController
1.設(shè)置tabbar上面的字體樣式
- #pragma mark - 設(shè)置tabbar上面的字 setTitleTextAttributes
- - (void)setTabBarTitleAttributesStyle
- {
- NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
- //通常字體大小
- attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
- //通常字體顏色
- attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
- NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
- //選中字體大小
- selectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
- //選中字體顏色
- selectAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
- //UI_APPEARANCE_SELECTOR 外觀
- UITabBarItem *item = [UITabBarItem appearance];
- //tabbar通常字體樣式
- [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
- //tabbar選中字體樣式
- [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
- }
2.導(dǎo)航子視圖封裝
- #pragma mark - 導(dǎo)航子視圖封裝
- - (void)setChildVC:(UIViewController *)ChildVC title:(NSString *)title image:(NSString *)image selectImgage:(NSString *)selectImage {
- //注意視圖層級關(guān)系,最上邊時UINavigationController,創(chuàng)建一個視圖即給一個UINavigationController
- NPNavigationController *nav = [[NPNavigationController alloc] initWithRootViewController:ChildVC];
- //子視圖顯示在UITabbarController上顯示的tabbar標(biāo)題
- nav.tabBarItem.title = title;
- //子視圖顯示在UITabbarController上未選中tabbar的圖片
- nav.tabBarItem.image = [UIImage imageNamed:image];
- //子視圖顯示在UITabbarController上選中tabbar的圖片
- nav.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
- //子視圖背景顏色
- nav.view.backgroundColor = [UIColor grayColor];
- //注意標(biāo)題顯示是子視圖的標(biāo)題,非UINavigationController
- ChildVC.navigationItem.title = title;
- //添加子視圖
- [self addChildViewController:nav];
- }
二.繼承UINavigationController創(chuàng)建NPNavigationController
1.重寫-(void)pushVewController:(UIViewConntroller*)viewCOntroller animated:(Bool)animated方法
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
- //判斷self.childViewControllers是不是拿到所有子控制器,self.childViewControllers.count為0時,只有子控制器,干掉返回按鈕
- if (self.childViewControllers.count > 0) {
- //自定義返回按鈕
- UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- //設(shè)置title:返回
- [returnBtn setTitle:@"返回" forState:UIControlStateNormal];
- //正常下image
- [returnBtn setImage:[UIImage imageNamed:@"black"] forState:UIControlStateNormal];
- //高亮下image
- [returnBtn setImage:[UIImage imageNamed:@"grat"] forState:UIControlStateHighlighted];
- //正常下title顏色
- [returnBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- //高亮下title顏色
- [returnBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
- //設(shè)置按鈕位置大小,注意這里位置無效果
- [returnBtn setFrame:CGRectMake(0, 0, 70, 20)];
- //按鈕內(nèi)部對齊
- [returnBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
- //設(shè)置圖片內(nèi)部位置
- [returnBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
- //添加點(diǎn)擊事件
- [returnBtn addTarget:self action:@selector(returnBtnClick) forControlEvents:UIControlEventTouchUpInside];
- //當(dāng)視圖推送時隱藏tabbar
- viewController.hidesBottomBarWhenPushed = YES;
- //自定義push視圖的leftBarButtonItem
- viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
- }
- //子控制器通過這個方法推送視圖
- [super pushViewController:viewController animated:animated];
三.添加UIBarButtonItem的類擴(kuò)展UIBarButtonItem+NPBarbutton,創(chuàng)建UIbarbuttonItem類方法
1.UIbarbuttonItem類方法
- /**
- *創(chuàng)建一個UIBarbuttonItem 類方法,這個方法是類方法創(chuàng)建一個按鈕,傳入的參數(shù):image正常顯示的圖片,hightImage高亮情況下顯示的圖片,target用的對象,action點(diǎn)擊的行為。
- */
- + (instancetype)itemWithImage:(NSString *)image hightImage:(NSString *)hightImage target:(id)target action:(SEL)action
- {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
- [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateNormal];
- //btnsize 是北京圖片的大小
- CGSize btnsize = btn.currentBackgroundImage.size;
- //設(shè)置btn的frame
- [btn setFrame:CGRectMake(0, 0, btnsize.width, btnsize.height)];
- [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
- return [[self alloc] initWithCustomView:btn];
- }
以上為基礎(chǔ)框架主要實(shí)現(xiàn),還有一些不足,手勢滑動返回沒有寫到。
推廣MarkDown語法鏈接:http://www.jianshu.com/p/7cc9c26e8b7a 作者:大毛集團(tuán)
本文Demo下載地址:http://code.cocoachina.com/view/129999 作者:NiePlus