UITabBarController自定义二之xib

UITabBarController自定义二之xib

  • 新建一个xib文件
    Alt text

  • 在UITabBarController的子类方法viewDidLoad方法中加载xib

1.-(void)viewDidLoad{
2. [super viewDidLoad];
3. self.tabBar.hidden = YES;
4.
5. _customTabBar = [[[NSBundle mainBundle] loadNibNamed:@"tabBar" owner:self options:nil] firstObject];
6.
7. CGRect frame = _customTabBar.frame;
8. frame.origin.y = self.view.bounds.size.height - frame.size.height;
9. _customTabBar.frame = frame;
10. [self.view addSubview:_customTabBar];
11.
12. for (UIView *view in _customTabBar.subviews) {
13. if ([view isKindOfClass:[UIButton class]]) {
14. UIButton *btn = (UIButton *)view;
15. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
16. }
17. }
18.}
19.
20.-(void)btnClick:(UIButton *)sender{
21.
22. for (UIView *view in _customTabBar.subviews) {
23. if ([view isKindOfClass:[UIButton class]]) {
24. UIButton *btn = (UIButton *)view;
25. btn.selected = NO;
26. }
27. }
28. self.selectedIndex = sender.tag - 200;
29. sender.selected = YES;
30.
31.
32.}
33.
  • 效果
    Alt text
 

你可能感兴趣的:(UITabBarController自定义二之xib)