一、判断当前iOS设备的类型和型号。
-(NSString*)deviceVersion{
// 需要#import "sys/utsname.h"
structutsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSStringstringWithCString:systemInfo.machineencoding:NSUTF8StringEncoding];
if ([deviceStringisEqualToString:@"iPhone1,1"])return @"iPhone 2G";
if ([deviceStringisEqualToString:@"iPhone1,2"])return @"iPhone 3G";
if ([deviceStringisEqualToString:@"iPhone2,1"])return @"iPhone 3GS";
if ([deviceStringisEqualToString:@"iPhone3,1"])return @"iPhone 4";
if ([deviceStringisEqualToString:@"iPhone3,2"])return @"iPhone 4";
if ([deviceStringisEqualToString:@"iPhone3,3"])return @"iPhone 4";
if ([deviceStringisEqualToString:@"iPhone4,1"])return @"iPhone 4S";
if ([deviceStringisEqualToString:@"iPhone5,1"])return @"iPhone 5";
if ([deviceStringisEqualToString:@"iPhone5,2"])return @"iPhone 5";
if ([deviceStringisEqualToString:@"iPhone5,3"])return @"iPhone 5c";
if ([deviceStringisEqualToString:@"iPhone5,4"])return @"iPhone 5c";
if ([deviceStringisEqualToString:@"iPhone6,1"])return @"iPhone 5s";
if ([deviceStringisEqualToString:@"iPhone6,2"])return @"iPhone 5s";
if ([deviceStringisEqualToString:@"iPhone7,1"])return@"iPhone 6 Plus";
if ([deviceStringisEqualToString:@"iPhone7,2"])return @"iPhone 6";
if ([deviceStringisEqualToString:@"iPhone8,1"])return @"iPhone 6s";
if ([deviceStringisEqualToString:@"iPhone8,2"])return@"iPhone 6s Plus";
if ([deviceStringisEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceStringisEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceStringisEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceStringisEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceStringisEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
if ([deviceStringisEqualToString:@"iPod6,1"]) return @"iPod Touch 6G";
if ([deviceStringisEqualToString:@"iPod7,1"]) return @"iPod Touch 7G";
if ([deviceStringisEqualToString:@"iPad1,1"]) return @"iPad 1G";
if ([deviceStringisEqualToString:@"iPad2,1"]) return @"iPad 2";
if ([deviceStringisEqualToString:@"iPad2,2"]) return @"iPad 2";
if ([deviceStringisEqualToString:@"iPad2,3"]) return @"iPad 2";
if ([deviceStringisEqualToString:@"iPad2,4"]) return @"iPad 2";
if ([deviceStringisEqualToString:@"iPad2,5"]) return @"iPad Mini 1G";
if ([deviceStringisEqualToString:@"iPad2,6"]) return @"iPad Mini 1G";
if ([deviceStringisEqualToString:@"iPad2,7"]) return @"iPad Mini 1G";
if ([deviceStringisEqualToString:@"iPad3,1"]) return @"iPad 3";
if ([deviceStringisEqualToString:@"iPad3,2"]) return @"iPad 3";
if ([deviceStringisEqualToString:@"iPad3,3"]) return @"iPad 3";
if ([deviceStringisEqualToString:@"iPad3,4"]) return @"iPad 4";
if ([deviceStringisEqualToString:@"iPad3,5"]) return @"iPad 4";
if ([deviceStringisEqualToString:@"iPad3,6"]) return @"iPad 4";
if ([deviceStringisEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([deviceStringisEqualToString:@"iPad4,2"]) return @"iPad Air ";
if ([deviceStringisEqualToString:@"iPad4,3"]) return @"iPad Air ";
if ([deviceStringisEqualToString:@"iPad4,4"]) return @"iPad Mini 2G";
if ([deviceStringisEqualToString:@"iPad4,5"]) return @"iPad Mini 2G";
if ([deviceStringisEqualToString:@"iPad4,6"]) return @"iPad Mini 2G";
if ([deviceStringisEqualToString:@"iPad4,7"]
||[deviceStringisEqualToString:@"iPad4,8"]
||[deviceStringisEqualToString:@"iPad4,9"])return@"iPad mini 3";
if ([deviceStringisEqualToString:@"i386"]) return @"iPhone Simulator";
if ([deviceStringisEqualToString:@"x86_64"]) return @"iPhone Simulator";
elsereturn@"";
return deviceString;
}
二、获取当前设备系统版本
[[[[UIDevicecurrentDevice]systemVersion]componentsSeparatedByString:@"."][0]intValue] >=7;
三、计算日期时间间隔(可用于根据用户的生日来计算年龄)
//创建日期格式化对象
NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//创建了两个日期对象
NSDate *date1=[dateFormatterdateFromString:@"2010-3-3 11:00"];
NSDate *date2=[dateFormatterdateFromString:@"2010-3-4 12:00"];
//NSDate *date=[NSDate date];
//NSString *curdate=[dateFormatter stringFromDate:date];
//取两个日期对象的时间间隔:
//这里的NSTimeInterval并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
int days=((int)time)/(3600*24);
int hours=((int)time)%(3600*24)/3600;
NSString *dateContent=[[NSStringalloc]initWithFormat:@"%i天%i小时",days,hours];
先将标准时间(格式如:1990-12-12)转时间戳,再计算出年龄
NSString *timeStr = [NSStringstringWithFormat:@"%@",self.medicalRecord.birthday];
NSDateFormatter *formatter =[[NSDateFormatteralloc]init];
[formattersetDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatterdateFromString:timeStr];
NSString *timeSp = [NSStringstringWithFormat:@"%ld",(long)[datetimeIntervalSince1970]];
NSDate *confromTimesp = [NSDatedateWithTimeIntervalSince1970:[timeSpintegerValue]];
NSString *age = [NSStringstringWithFormat:@"%@",[NSDategetAgeByBirthDay:confromTimesp]];//计算患者年龄
计算年龄的方法:
//年龄计算
+(NSString*)getAgeByBirthDay:(NSDate*)birthDay{
NSDate *birthday = birthDay;
NSDate *today = [NSDatedate];
NSCalendar *gregorian = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregoriancomponents:NSYearCalendarUnitfromDate:birthdaytoDate:todayoptions:0];
return [NSStringstringWithFormat:@"%zi",components.year];
}
四、解决xib加载时,内部空间位置(跑偏)问题(适用于将xib文件装到另外一个父视图里面的情况,比如此处为将topView装入到header中,作为tableView的头视图);
UIView *header = [[UIViewalloc]initWithFrame:CGRectMake(0,0,WIDTH,200)];
self.tableView.tableHeaderView = header;
PHomeTopMenuView *topView = [[[NSBundlemainBundle]loadNibNamed:@"PHomeTopMenuView"owner:selfoptions:nil]firstObject];
topView.frame = header.bounds;
//直接设置xib文件的frame=CGRectMake(0, 0, WIDTH, 200)的话,可能会使xib内部的约束显示跑偏,所以可以直接将其frame设置为父视图的bounds。
五、重新定义图片大小
-(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[imagedrawInRect:CGRectMake(0,0, reSize.width, reSize.height)];
UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
在XCode的项目的resource文件夹中追加名为Default.png的图片就可以了。你可以使用Organizer工 具来抓取iPhone实机上图片。
Organizer是XCode中内含的一个工具,通过选择菜单XCode -> Window -> Organizer来启动。这样就会在程序加载前的呢一会黑屏中运行该画面。
http://www.print100.com/hk/html/csi.aspx
九、改变状态栏颜色(白底黑字)
UIView *statusBarView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,WIDTH,20)];
statusBarView.backgroundColor=[UIColorblackColor];
[self.viewaddSubview:statusBarView];
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleDefaultanimated:NO];
十、UISearchDisplayController 无搜索结果下的文字定义
现在需要修改UISearchDisplayController 在搜索不到数据的情况下,显示为:
需要更改红框里面的内容,调用此方法以下方法:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{ for(UIView *subview in self.searchDisplay.searchResultsTableView.subviews) {
if([subview isKindOfClass:[UILabel class]]) {
[(UILabel*)subview setText:@"XXXXXXX"];
}
}
return YES;
}
[(UILabel*)subview setText:@"XXXXXXX"];输入你想要定义的内容,就可以达到你想要的效果了
十一、lib库所支持的框架类型:i386表示支持iphone模拟器,armv6应用在iphone 3gs上,armv7应用在iphone4,armv7s应用在iphone4s以及iphone5,arm64应用在最新的iphone5s上。
十二、双击手势
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doDoubleTap)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:doubleTap];
十三、解决输入文字不从顶部开始的bug(例如:可用于解决UITextView中文字不从顶部开始显示,而是在中间开始显示文字的问题)
self.automaticallyAdjustsScrollViewInsets = NO;
十四、给tabbar添加badgeValue
int indexICareAbout = 2;
NSString *badgeValue = @"10";
[[[[[self tabBarController] viewControllers] objectAtIndex: indexICareAbout] tabBarItem] setBadgeValue:badgeValue];
十五、block弱引用释义:__weak__typeof(self) weakself = self;
此句代码,声明了一个self类型的 weak指针 weakSelf ,typeof(self) 是获取到self的类型,这样定义出的weakSelf就是和self一个类型的, 加上__weak是建立一个若引用,整句就是给self定义了一个若引用性质的替身;这个一般用在使用block时会用到,因为block会copy它内部的变量,可能会造成引用循环,使用__weak性质的self替代self,可以切断block对self的引用,避免循环引用 。
另外需要注意的是:在block内部引用实例变量要用weakSelf->实例变量 的方式来访问,而不能用原来的形式self.XXX进行访问,否则会造成block对self的强引用,从而引起循环引用。
十六、键盘处理
#pragma mark---监听键盘活动
- (void)viewLoad
{
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat height=keyboardRect.size.height;
_TableView.frame=CGRectMake(0, 115, SCREEN_WIDTH, SCREEN_HEIGHT-115-50-40-height);
_DownView.frame=CGRectMake(0, SCREEN_HEIGHT-90-height, SCREEN_WIDTH, 90);
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
_TableView.frame=CGRectMake(0, 115, SCREEN_WIDTH, SCREEN_HEIGHT-115-50-40);
_DownView.frame=CGRectMake(0, SCREEN_HEIGHT-90, SCREEN_WIDTH, 90);
}
十七、向上取整 (当数据不确定时,可用于返回UITableViewCell的高度)
ceil(dataArray.count/4.0);如:结果是"1.65",则会自动向上取到"2".
十八、直接从SDWebImage的缓存中取图片
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgUrl];
十九、伪横屏
//隐藏系统状态栏,并添加“伪横屏”
[[UIApplicationsharedApplication]setStatusBarHidden:YESwithAnimation:UIStatusBarAnimationFade];
[UIViewanimateWithDuration:0.35delay:0options:UIViewAnimationOptionTransitionCrossDissolveanimations:^{
CGAffineTransform transform =CGAffineTransformIdentity;
transform =CGAffineTransformRotate(transform,M_PI/2);
self.view.transform = transform;
} completion:NULL];
二十 修改uitextfield的placeholder字体和位置
自定义一个textfield,继承于uitextfield,并重写下面这个方法:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}
func scrollViewDidScroll(_ scrollView:UIScrollView) {
if scrollView.contentOffset.y>oldOff_y{
self.addBtn.isHidden=true
}else{
self.addBtn.isHidden=false
}
oldOff_y = scrollView.contentOffset.y//将当前位移变成缓存位移
}
二十二、创建假的状态栏
-(void)setupNavBar{
//创建一个高20的假状态栏
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width, 20)];
//设置成蓝色
statusBarView.backgroundColor=[UIColor colorWithRed:49/255.0f green:171/255.0f blue:248/255.0f alpha:1];
// 添加到 navigationBar 上
[self.view addSubview:statusBarView];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
}