如何搭建一个框架
在做百思的项目过程中,发现导航控制器和标签控制器的集成是一个不变的流程,只要是这样的框架都可以这样
创建标签栏子控制器
/** 这里是创建子控制器的方法,说到方法抽取我们应该讲变化的东西变参数,不变的抽到方法里 */
- (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修改系统的tabBar
// 改用自己的TabBar
BSTabBar *tabBar = [[BSTabBar alloc] init];
[self setValue:tabBar forKeyPath:@"tabBar"];
自定义tabBar
// 创建的时候调用
- (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;
}
// 修改尺寸时调用
- (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) {
// 属于这个类型就改变位置,这里是反射机制,这个字符串对应的类名!
if (([btn isKindOfClass:NSClassFromString(@"UITabBarButton")])) {
CGFloat btnX = btnW * (index > 1 ? index + 1 : index);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
index ++;
}
}
}
initialize 这个类方法只会被调用一次
+ (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];
}
当一个方法或属性后面有UI_APPEARANCE_SELECTOR标识的时候,都可以用appearance
// 设置导航栏上的背景图片, 用appearance,以后所有的地方都用这个背景
UINavigationBar *bar = [UINavigationBar appearance];
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
这里是要实现导航栏上的返回按钮统一
// 重写push方法, 用来拦截push进来的视图
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 这里判断是否进入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];
// 设置按钮内容左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 内边距
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;
}
// 这里写在下面,给外面权利修改按钮
[super pushViewController:viewController animated:animated];
}
有时候是不要继承某个控件做事情的,写个分类就好
+ (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];
网友评论