如何搭建一個(gè)框架
在做百思的項(xiàng)目過程中,發(fā)現(xiàn)導(dǎo)航控制器和標(biāo)簽控制器的集成是一個(gè)不變的流程,只要是這樣的框架都可以這樣
創(chuàng)建標(biāo)簽欄子控制器
/** 這里是創(chuàng)建子控制器的方法,說到方法抽取我們應(yīng)該講變化的東西變參數(shù),不變的抽到方法里 */
- (void)setUpChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
vc.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
BSNavigationController *nav = [[BSNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
自定義tabBar我們可以用KVC修改系統(tǒng)的tabBar
// 改用自己的TabBar
BSTabBar *tabBar = [[BSTabBar alloc] init];
[self setValue:tabBar forKeyPath:@"tabBar"];
自定義tabBar
// 創(chuàng)建的時(shí)候調(diào)用
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
UIButton *publish = [UIButton buttonWithType:UIButtonTypeCustom];
[publish setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publish setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
[self addSubview:publish];
self.publish = publish;
}
return self;
}
// 修改尺寸時(shí)調(diào)用
- (void)layoutSubviews {
[super layoutSubviews];
self.publish.frame = CGRectMake(0, 0, self.publish.currentImage.size.width, self.publish.currentImage.size.height);
self.publish.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
CGFloat btnY = 0;
CGFloat btnW = self.frame.size.width / 5;
CGFloat btnH = self.frame.size.height;
NSInteger index = 0;
for (UIView *btn in self.subviews) {
// 屬于這個(gè)類型就改變位置,這里是反射機(jī)制,這個(gè)字符串對(duì)應(yīng)的類名!
if (([btn isKindOfClass:NSClassFromString(@"UITabBarButton")])) {
CGFloat btnX = btnW * (index > 1 ? index + 1 : index);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
index ++;
}
}
}
initialize 這個(gè)類方法只會(huì)被調(diào)用一次
+ (void)initialize {
/** 修改tabBar上面的字體顏色和大小 */
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *sattrs = [NSMutableDictionary dictionary];
sattrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:sattrs forState:UIControlStateSelected];
}
當(dāng)一個(gè)方法或?qū)傩院竺嬗蠻I_APPEARANCE_SELECTOR標(biāo)識(shí)的時(shí)候,都可以用appearance
// 設(shè)置導(dǎo)航欄上的背景圖片, 用appearance,以后所有的地方都用這個(gè)背景
UINavigationBar *bar = [UINavigationBar appearance];
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
這里是要實(shí)現(xiàn)導(dǎo)航欄上的返回按鈕統(tǒng)一
// 重寫push方法, 用來攔截push進(jìn)來的視圖
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 這里判斷是否進(jìn)入push視圖
if (self.childViewControllers.count > 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
btn.size = CGSizeMake(70, 30);
[btn setTitleColor:BSColor(2, 2, 2) forState:UIControlStateNormal];
[btn setTitleColor:BSColor(255, 60, 48) forState:UIControlStateHighlighted];
// 設(shè)置按鈕內(nèi)容左對(duì)齊
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 內(nèi)邊距
btn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
// 隱藏tabTar
viewController.hidesBottomBarWhenPushed = YES;
}
// 這里寫在下面,給外面權(quán)利修改按鈕
[super pushViewController:viewController animated:animated];
}
有時(shí)候是不要繼承某個(gè)控件做事情的,寫個(gè)分類就好
+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
button.size = button.currentBackgroundImage.size;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[self alloc] initWithCustomView:button];