一些开发的小技巧分享~
1、PrefixHeader导入位置写法
$(SRCROOT)/$(PROJECT_NAME)/<#文件所在文件夹名称#>/PrefixHeader.pch
2、隐藏导航栏的两种方式
//方式一 《推荐》
//在需要隐藏的viewController中遵循<UINavigationControllerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航控制器的代理为self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判断要显示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
//方式二
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
3、TabBarItem要求只显示图片不显示文字
// self 表示需要设置样式的控制器
[self.tabBarItem setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
4、TableView或CollectionView单选
<!--步骤一、 声明一个全局变量-->
@property (nonatomic, assign) NSInteger selectedIndex;
<!--步骤二、TableView或CollectionView点击事件的方法中保存点击的index,并刷新-->
self.selectedIndex = indexPath.row;
[collectionView reloadData];或[tableView reloadData];
<!--步骤三、在cellForItemAtIndexPath或cellForRowAtIndexPath方法中判断是否是所保存的index-->
if (self.selectedIndex == indexPath.row) {
// 设置选中样式
} else {
// 设置成默认样式
}
5、怎么区分真机还是模拟器
#if TARGET_IPHONE_SIMULATOR
NSLog(@"run on simulator");
#define SIMULATOR_TEST
#else
//不定义SIMULATOR_TEST这个宏
NSLog(@"run on device");
#endif
6、if Debug
#ifdef DEBUG
// do sth
#else
// do sth
#endif
7、一键清理所有模拟器安装过的APP
// 先关闭所有模拟器 在终端中输入下面命令即可
xcrun simctl erase all
8、SDWebImage清理缓存
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[[[SDWebImageManager sharedManager] imageCache] clearMemory];
}
9、UITableView的HeaderView下拉放大
1、View1:作为显示View
2、View2:作为tableView的tableHeaderView,并添加View1,View2的大小和View1大小相同.
3、在UIScrollViewDelegate方法中进行设计
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat width = UIScreen.mainScreen.bounds.size.width;
CGFloat viewHeight = kScreenWidth * 0.5; // View1的初始高度
CGFloat yOffset = scrollView.contentOffset.y;
if (yOffset < 0) {
CGFloat totalOffset = viewHeight + ABS(yOffset);
CGFloat f = totalOffset / viewHeight;
self.headerView.frame = CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset);
}
}
10、Masonry制作就宫格
//Objective-C ----> Masonry
/**
多视图布局
@param viewArray 视图数组
@param column 列数
@param tbSpeace 视图上下间距
@param lrSpeace 视图左右间距
@param topSpeace 和父视图上间距
@param lrSuperViewSpeace 父视图左右间距
@param superView 父视图
@param viewHeight 视图高度
*/
- (void)wd_masLayoutSubViewsWithViewArray:(NSArray<UIView *> *)viewArray
columnOfRow:(NSInteger)column
topBottomOfItemSpeace:(CGFloat)tbSpeace
leftRightItemSpeace:(CGFloat)lrSpeace
topOfSuperViewSpeace:(CGFloat)topSpeace
leftRightSuperViewSpeace:(CGFloat)lrSuperViewSpeace
addToSubperView:(UIView *)superView
viewHeight:(CGFloat)viewHeight{
CGFloat viewWidth = superView.bounds.size.width;
CGFloat itemWidth = (viewWidth - lrSuperViewSpeace * 2 - (column - 1) * lrSpeace) / column * 1.0f;
CGFloat itemHeight = viewHeight;
UIView *last = nil;
for (int i = 0; i < viewArray.count; i++) {
UIView *item = viewArray[i];
[superView addSubview:item];
[item mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(itemWidth);
make.height.mas_equalTo(itemHeight);
CGFloat top = topSpeace + (i / column) * (itemHeight + tbSpeace);
make.top.mas_offset(top);
if (!last || (i % column) == 0) {
make.left.mas_offset(lrSuperViewSpeace);
}else{
make.left.mas_equalTo(last.mas_right).mas_offset(lrSpeace);
}
}];
last = item;
}
}
11、Snapkit制作九宫格
//Swift ----> SnapKit
/// 多视图布局
///
/// - Parameters:
/// - viewArray: 视图数组
/// - columnOfRow: 列数
/// - topBottomOfItemSpeace: 视图上下间距
/// - leftRightItemSpeace: 视图左右间距
/// - topOfSuperViewSpeace: 和父视图上间距
/// - leftRightSuperViewSpeace: 父视图左右间距
/// - addToSubperView: 父视图
/// - viewHeight: 视图高度
func wd_masLayoutSubViews(viewArray:Array<UIView>,
columnOfRow:Int,
topBottomOfItemSpeace:CGFloat,
leftRightItemSpeace:CGFloat,
topOfSuperViewSpeace:CGFloat,
leftRightSuperViewSpeace:CGFloat,
addToSubperView:UIView,
viewHeight:CGFloat) -> Void {
let viewWidth = addToSubperView.bounds.width
let tempW = leftRightSuperViewSpeace * 2 + CGFloat(columnOfRow - 1) * leftRightItemSpeace
let itemWidth = (viewWidth - tempW) / CGFloat(columnOfRow)
let itemHeight = viewHeight
print(leftRightSuperViewSpeace,leftRightItemSpeace,itemWidth)
var lastView:UIView?
for (index) in viewArray.enumerated() {
let item = viewArray[i]
addToSubperView.addSubview(item)
item.snp.makeConstraints { (make) in
make.width.equalTo(itemWidth)
make.height.equalTo(itemHeight)
let top = topOfSuperViewSpeace + CGFloat(i / columnOfRow) * (itemHeight + topBottomOfItemSpeace)
make.top.equalTo(top)
if !(lastView != nil) || i%columnOfRow == 0 {
make.left.equalTo(leftRightSuperViewSpeace)
} else {
make.left.equalTo((lastView?.snp.right)!).offset(leftRightItemSpeace)
}
lastView = item
}
}
}
12、沉浸式TableView(ScrollView、CollectionView)
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = false;
}
13、通过身份证计算年龄
/** 通过身份证计算年龄 */
- (NSString *)calculationAgeWithBirthday: (NSString *)birthday {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *nowDate = [NSDate date];
NSString *birth = birthday;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *birthDay = [dateFormatter dateFromString:birth];
unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *date = [calendar components:unitFlags fromDate:birthDay toDate:nowDate options:0];
if ([date year] > 0) {
return [NSString stringWithFormat:@"%ld岁", (long)[date year]];
} else if([date month] > 0) {
return [NSString stringWithFormat:@"%ld月", (long)[date month]];
} else if([date day] > 0){
return [NSString stringWithFormat:@"%ld天", (long)[date day]];
} else {
return @"0天";
}
}
- (NSString *)ageStrFromIdentityCard:(NSString *)numberStr {
NSString *dateSt;
NSMutableString *dateS;
if (numberStr.length > 15) {
dateSt = [numberStr substringWithRange:NSMakeRange(6, 8)];
dateS = [NSMutableString stringWithFormat:@"%@", dateSt];
[dateS insertString:@"-" atIndex:4];
[dateS insertString:@"-" atIndex:7];
} else {
// 只考虑 19开头的15位的身份证号
dateSt = [NSString stringWithFormat:@"19%@",[numberStr substringWithRange:NSMakeRange(6, 6)]];
dateS = [NSMutableString stringWithFormat:@"%@", dateSt];
[dateS insertString:@"-" atIndex:4];
[dateS insertString:@"-" atIndex:7];
}
return [self calculationAgeWithBirthday:dateS];
}
14、利用runtime
自动归档
// 导入头文件
#import <objc/runtime.h>
// 遵循协议
<NSCoding>
// 实现方法
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int outCount;
Ivar * ivars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar ivar = ivars[i];
NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int outCount;
Ivar * ivars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar ivar = ivars[i];
NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
[aCoder encodeObject:[self valueForKey:key] forKey:key];
}
}
网友评论