前言
? hello 大家好,記錄一些常用和不常用的iOS知識(shí)點(diǎn)赁项,防止遺忘丟失葛躏,如果你有更好的方法請(qǐng)告訴我,互相學(xué)習(xí)悠菜。
一舰攒、調(diào)用代碼使APP進(jìn)入后臺(tái),達(dá)到點(diǎn)擊Home鍵的效果悔醋。(私有API)
[[UIApplication sharedApplication] performSelector:@selector(suspend)];
suspend的英文意思有:暫停; 懸; 掛; 延緩;
二摩窃、帶有中文的URL處理。
編碼
NSString* string1 = @"https://www.cloudsafe.com/文件夾";
NSString*?string2?=?[string1?stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
解碼
NSString* string3 = [string2 stringByReplacingPercentEscapesUsingEncoding: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設(shè)置圖片(UILabel一樣適用)
第一種方法
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;
//設(shè)置顯示的圖片范圍
yourView.layer.contentsCenter = CGRectMake(0.25,0.25,0.5,0.5);//四個(gè)值在0-1之間,對(duì)應(yīng)的為x德玫,y,width椎麦,height宰僧。
五、去掉UITableView多余的分割線(數(shù)據(jù)未充滿屏幕)
tableView.tableFooterView = [UIView new];
六观挎、UITableView收起鍵盤
何必這么麻煩琴儿,一個(gè)屬性搞定,效果好(UIScrollView同樣可以使用)
以前是不是覺得[self.view endEditing:YES];很屌嘁捷,這個(gè)下面的更屌造成。
yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
另外一個(gè)枚舉為UIScrollViewKeyboardDismissModeInteractive,表示在鍵盤內(nèi)部滑動(dòng)雄嚣,鍵盤逐漸下去晒屎。
七、獲取今天是星期幾
+ (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;
}
return weekDayString;
八鼓鲁、UIView的部分圓角問題
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
//指定了需要成為圓角的角。該參數(shù)是UIRectCorner類型的港谊,可選的值有:
* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners
九骇吭、設(shè)置滑動(dòng)的時(shí)候隱藏navigationBar
self.navigationController.hidesBarsOnSwipe = YES;
十、自動(dòng)布局中多行UILabel歧寺,需要設(shè)置其preferredMaxLayoutWidth屬性才能正常顯示多行內(nèi)容燥狰。另外如果出現(xiàn)顯示不全文本棘脐,可以在計(jì)算的結(jié)果基礎(chǔ)上+0.5。
CGFloat h = [model.message boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height+0.5;