记录一些常用和不常用的iOS知识点,防止遗忘丢失。(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正;如有好的知识点,也欢迎大家联系我,添加上去。谢谢!
一、调用代码使APP进入后台,达到点击Home键的效果。(私有API)
[[UIApplication sharedApplication] performSelector:@selector(suspend)];
suspend的英文意思有:暂停; 悬; 挂; 延缓;
二、带有中文的URL处理。
大概举个例子,类似下面的URL,里面直接含有中文,可能导致播放不了,那么我们要处理一个这个URL,因为他太操蛋了,居然用中文。
http://static.tripbe.com/videofiles/视频/我的自拍视频.mp4
NSString *path = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)model.mp4_url, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
三、获取UIWebView的高度
个人最常用的获取方法,感觉这个比较靠谱
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
CGRect frame = webView.frame;
webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);
}
四、给UIView设置图片(UILabel一样适用)
-
第一种方法:
利用的UIView的设置背景颜色方法,用图片做图案颜色,然后传给背景颜色。
UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bgImg.png"];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[myView setBackGroundColor:bgColor];
-
第二种方法:
UIImage *image = [UIImage imageNamed:@"yourPicName@2x.png"];
yourView.layer.contents = (__bridge id)image.CGImage;
//设置显示的图片范围
yourView.layer.contentsCenter = CGRectMake(0.25,0.25,0.5,0.5);//四个值在0-1之间,对应的为x,y,width,height。
五、去掉UITableView多余的分割线
yourTableView.tableFooterView = [UIView new];
六、调整cell分割线的位置,两个方法一起用,暴力解决,防脱发
-(void)viewDidLayoutSubviews {
if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {
[self.mytableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)]) {
[self.mytableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
}
}
#pragma mark - cell分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
}
}
七、UILabel和UIImageView的交互userInteractionEabled默认为NO。那么如果你把这两个类做为父试图的话,里面的所有东东都不可以点击哦。曾经有一个人,让我帮忙调试bug,他调试很久没搞定,就是把WMPlayer对象(播放器对象)放到一个UIImageView上面。这样imageView addSubView:wmPlayer 后,播放器的任何东东都不能点击了。userInteractionEabled设置为YES即可。
八、UISearchController和UISearchBar的Cancle按钮改title问题,简单粗暴
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
searchController.searchBar.showsCancelButton = YES;
UIButton *canceLBtn = [searchController.searchBar valueForKey:@"cancelButton"];
[canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
[canceLBtn setTitleColor:[UIColor colorWithRed:14.0/255.0 green:180.0/255.0 blue:0.0/255.0 alpha:1.00] forState:UIControlStateNormal];
searchBar.showsCancelButton = YES;
return YES;
}
九、UITableView收起键盘何必这么麻烦
一个属性搞定,效果好(UIScrollView同样可以使用)
以前是不是觉得[self.view endEditing:YES];很屌,这个下面的更屌。
yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
另外一个枚举为UIScrollViewKeyboardDismissModeInteractive,表示在键盘内部滑动,键盘逐渐下去。
十、NSTimer
1、NSTimer计算的时间并不精确
2、NSTimer需要添加到runLoop运行才会执行,但是这个runLoop的线程必须是已经开启。
3、NSTimer会对它的tagert进行retain,我们必须对其重复性的使用intvailte停止。target如果是self(指UIViewController),那么VC的retainCount+1,如果你不释放NSTimer,那么你的VC就不会dealloc了,内存泄漏了。
十一、UIViewController没用大小(frame)
经常有人在群里问:怎么改变VC的大小啊?
瞬间无语。(只有UIView才能设置大小,VC是控制器啊,哥!)
十二、用十六进制获取UIColor(类方法或者Category都可以,这里我用工具类方法)
+ (UIColor *)colorWithHexString:(NSString *)color
{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// strip 0X if it appears
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
十三、获取今天是星期几
+ (NSString *) getweekDayStringWithDate:(NSDate *) date
{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法
NSDateComponents *comps = [calendar components:NSWeekdayCalendarUnit fromDate:date];
// 1 是周日,2是周一 3.以此类推
NSNumber * weekNumber = @([comps weekday]);
NSInteger weekInt = [weekNumber integerValue];
NSString *weekDayString = @"(周一)";
switch (weekInt) {
case 1:
{
weekDayString = @"(周日)";
}
break;
case 2:
{
weekDayString = @"(周一)";
}
break;
case 3:
{
weekDayString = @"(周二)";
}
break;
case 4:
{
weekDayString = @"(周三)";
}
break;
case 5:
{
weekDayString = @"(周四)";
}
break;
case 6:
{
weekDayString = @"(周五)";
}
break;
case 7:
{
weekDayString = @"(周六)";
}
break;
default:
break;
}
return weekDayString;
}