开发实用整理

  • 自动补全目录,在文件写上 $(SRCROOT)
$(SRCROOT)/BiShe/PrefixHeader.pch
  • 自定义分栏控制器 UITabBarController
@implementation BSTabBarController

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 统一设置Item的文字属性
        [self setUpItemTextAttrs];

        // 添加所以子控制器
        [self setUpAllChildViewControllers];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

     统一设置Item的文字属性
//    [self setUpItemTextAttrs];
//    
//    // 添加所以子控制器
//    [self setUpAllChildViewControllers];
}

/**
 *  统一设置Item文字的属性
 */
- (void)setUpItemTextAttrs{
    // 统一设置Item文字的属性
    // UI_APPEARANCE_SELECTOR
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    normalAttrs[NSFontAttributeName] = Font_20;

    // 选中状态
    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
    selectAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];

    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
}

/**
 *  添加所有子控制器
 */
- (void)setUpAllChildViewControllers{


    [self setUpOneViewController:[[BSHomeViewController alloc]init] title:@"首页" image:@"tabbar_home_nor" selectImage:@"tabbar_home_pre"];

    [self setUpOneViewController:[[BSCategoryViewController alloc]init] title:@"分类" image:@"tabbar_category_nor" selectImage:@"tabbar_category_pre"];

    [self setUpOneViewController:[[BSShopCartViewController alloc]init] title:@"购物车" image:@"tabbar_shoppingcart_nor" selectImage:@"tabbar_shoppingcart_pre"];

    [self setUpOneViewController:[[BSAccountViewController alloc]init] title:@"个人中心" image:@"tabbar_mine_nor" selectImage:@"tabbar_mine_pre"];

}


/**
 *  添加一个子控制器
 */

- (void)setUpOneViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage
{
    if ([title isEqualToString:@"首页"]) {
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(0, 0, 40, 40);
        [imageView setImage:[UIImage imageNamed:@"home_logo"]];
        vc.navigationItem.titleView = imageView;
    }
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];

    BSNavigationController *nav = [[BSNavigationController alloc]initWithRootViewController:vc];

    [self addChildViewController:nav];
}

  • 继承UINavigationController的BSNavigationController,自定义导航栏,包括返回按钮,右滑返回手势
@interface BSNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation BSNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置pop手势代理
    self.interactivePopGestureRecognizer.delegate = self;

}

/**
 *  统一设置左上角返回图片
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {

        /* 自动显示和隐藏tabbar */
        viewController.hidesBottomBarWhenPushed = YES;

        // 设置左边的返回按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(blackBtnClick) image:@"commom_back" highImage:@"commom_back"];

    }
    [super pushViewController:viewController animated:animated];
}

- (void)blackBtnClick{

    [self popViewControllerAnimated:YES];

}

#pragma mark -- 点击空白处收起键盘
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

#pragma mark - <UIGestureRecognizerDelegate>

/**
 *  push进来的控制器大于1个,手势有效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    return self.viewControllers.count > 1;
}

@end

  • 导航字体设置
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:Font_30,NSForegroundColorAttributeName:Color_2F2F2F}];

  • 打印调试
#if DEBUG
#define DR_NSLog(fmt,...)    NSLog((@"%s [Line %d] " fmt),__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);  /**<输出语句*/
#else
#define DR_NSLog(fmt, ...)
#endif

  • 屏幕尺寸判断
// 判断 iPhone4s
#define kiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断 iPhone5
#define kiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iphone6
#define kiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iphone6+
#define kiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
  • 随机色
#define BSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define BSRandomColor BSColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
  • 重写UIBarButtonItem 分栏标题与图片
/**
 *  创建一个item
 *
 *  @param target    点击item后调用哪个对象的方法
 *  @param action    点击item后调用target的哪个方法
 *  @param image     图片
 *  @param highImage 高亮的图片
 *
 *  @return 创建完的item
 */
+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 30)];
    // 设置尺寸
    btn.size = CGSizeMake(60, 44);//btn.currentBackgroundImage.size;
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值