码迷,mamicode.com
首页 > 其他好文 > 详细

心得体会???

时间:2016-03-02 21:56:28      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

自定义

@interface KKTabBar : UITabBar
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBar : UIView
UITabBar 继承自 UIView
所以可以在 UITabBar 上添加view啦。
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_bottom"]];
        imgView.size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 49);
        [self addSubview:imgView];
        
        UIView *view = [[UIView alloc] init];
        view.size = CGSizeMake([UIScreen mainScreen].bounds.size.width/4, 2);
        view.x = 0;
        view.y = 0;
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        
        self.barTopView = view;
    }
    return self;
}

--------------
| UITabBarController |
--------------
@interface KKTabBarController : UITabBarController

@end
+ (void)initialize
{
   
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[NSForegroundColorAttributeName] = [UIColor purpleColor];
    
    NSMutableDictionary *selectDic = [[NSMutableDictionary alloc] init];
    selectDic[NSForegroundColorAttributeName] = [UIColor redColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:dic forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectDic forState:UIControlStateSelected];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    KKTabBar *tabBar = [[KKTabBar alloc] init];
    tabBar.delegate = self;
    [self setValue:tabBar forKey:@"tabBar"];

    KKHomeViewController *home = [[KKHomeViewController alloc] init];
    [self addVc:home title:@"主页" image:@"icon_home" selectImage:@"icon_home" tag:1];
    
    KKCollectController *collect = [[KKCollectController alloc] init];
    [self addVc:collect title:@"收藏" image:@"icon_fav" selectImage:@"icon_fav" tag:2];
    
    KKSearchController *search = [[KKSearchController alloc] init];
    [self addVc:search title:@"搜索" image:@"icon_search" selectImage:@"icon_search" tag:3];
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [board instantiateViewControllerWithIdentifier:@"moreController"];
//    KKMoreController *more = [[KKMoreController alloc] init];
    [self addVc:vc title:@"更多" image:@"icon_more" selectImage:@"icon_more" tag:4];
    
}

- (void)addVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage tag:(NSUInteger)tag{
    
    vc.title = title;
    // 放弃系统默认渲染方式,显示图片原本样式
//    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
    
    vc.tabBarItem.tag = tag;
    
    KKNavController *nav = [[KKNavController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
}

 

----------------
| UINavigationController |
----------------
@interface KKNavController : UINavigationController

@end
@implementation KKNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    UINavigationBar *item = [UINavigationBar appearance];
    [item setTitleTextAttributes:dic];
    
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"top"] forBarMetrics:UIBarMetricsDefault];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count>0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"back_hover"] forState:UIControlStateSelected];
        
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.size = CGSizeMake(50, 29);
        btn.backgroundColor = [UIColor lightGrayColor];
        
//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
        
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
 
    }
    
    [super pushViewController:viewController animated:animated];
    
}

- (void)back{
    
    [self popViewControllerAnimated:YES];
    
}

 

心得体会???

标签:

原文地址:http://www.cnblogs.com/kinghx/p/5236497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!