// LYTabBarController.m
@implementation LYTabBarController
#import "LYTabBarController.h"
#import "LYNavigationController.h"
#import "LYTabBar"
- (void)viewDidLoad {
[super viewDidLoad];
[self setupChildViewController:[[LYFirstViewController alloc] init] title:@"titleFirst" imageName:@"imageNameFirst" selectedImageName:@"selectedImageNameFirst"];
[self setupChildViewController:[[LYSecondViewController alloc] init] title:@"titleSecond" imageName:@"imageNameSecond" selectedImageName:@"selectedImageNameSecond"];
[self setupChildViewController:[[LYThirdViewController alloc] init] title:@"titleThird" imageName:@"imageNameThird" selectedImageName:@"selectedImageNameThird"];
[self setupChildViewController:[[LYFourthViewController alloc] init] title:@"titleFourth" imageName:@"imageNameFourth" selectedImageName:@"selctedImageNameFourth"];
// set my tabBar
[self setValue:[[LYTabBar alloc] init] forKeyPath:@"tabbar"];
}
- (void)setupChildViewController:(UIViewController)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
vc.navigationItem.title = title;
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:imageName];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
LYNavigationController *navigationController = [[LYNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:navigationController];
}
@end
// LYTabBar.m
#import "LYTabBar.h"
@interface LYTabBar()
/**publishButton**/
@propertity(nonatomic, weak) UIButton *publishButton;
@end
@implementation LYTabBar
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// set tabBar.backgroundImage
[self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImageName"]];
// set navigationBar.backgroundImage
// [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundImage"] forBarMetrics:UIBarMetrisDefault];
// [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundImage"] forBarMetrics:UIBarMetrisDefault];
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"]];
[self addSubview:publishButton];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// set publishButton.frame
self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
// set other UITabBarButton.frame
for (UIView *button in self.subviews) {
CGFloat buttonY = 0;
CGFloat buttonW = self.frame.size.width / 5;
CGFloat buttonH = self.frame.size.height;
NSInteger index = 0;
if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
// set buttonX
CGFloat buttonX = buttonW * ((index > 1) ? (index + 1) : index);
button.frame = CGRectMake(indexX, indexY, indexW, indexH);
}
}
@end