1.設(shè)置導(dǎo)航欄標(biāo)題顏色
UIColor *whiteColor = [UIColor redColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
2.設(shè)置view半透明倦蚪,subView不跟著變--通過設(shè)置背景色
UIView * backView = [[UIView alloc]initWithFrame:self.view.frame];
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
// backView.alpha = 0.3;//錯(cuò)誤做法
[self.view addSubview:backView];
UIButton * button = [[UIButton alloc]init];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(200, 200, 200, 200);
button.center = backView.center;
[backView addSubview:button];
3.設(shè)置VIew的部分圓角
CGRect rect = button.bounds;
CGSize radio = CGSizeMake(50, 50);//圓角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這只圓角位置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//創(chuàng)建shapelayer
masklayer.frame = button.bounds;
masklayer.path = path.CGPath;//設(shè)置路徑
button.layer.mask = masklayer;
4.數(shù)組快速求和侈离,平均值蹄葱,最大值峦筒,最小值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n向下取整%f\n向上取整%f\n%f\n%f\n%f",sum,floor(sum),ceil(sum),avg,max,min);
5.navigationBar根據(jù)滑動(dòng)距離的漸變色實(shí)現(xiàn)
//第一種
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetToShow = 200.0;//滑動(dòng)多少就完全顯示
CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;
}
//第二種
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetToShow = 200.0;
CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
}
//根據(jù)顏色生成圖片
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
4.數(shù)組快速求和究西,平均值,最大值物喷,最小值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n向下取整%f\n向上取整%f\n%f\n%f\n%f",sum,floor(sum),ceil(sum),avg,max,min);
7.獲取webView的高度
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
8.圖片緩存的清空(包括SD的卤材,可以同時(shí)清除session 和 cookie的緩存)
// 清理內(nèi)存
[[SDImageCache sharedImageCache] clearMemory];
// 清理webview 緩存
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config.URLCache removeAllCachedResponses];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// 清理硬盤
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.tableView reloadData];
}];
9.UIView 自帶動(dòng)畫翻轉(zhuǎn)界面
- (IBAction)changeImages:(id)sender
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_parentView cache:YES];
NSInteger purple = [[_parentView subviews] indexOfObject:self.image1];
NSInteger maroon = [[_parentView subviews] indexOfObject:self.image2];
[_parentView exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
10.工程中查看是否使用 IDFA
打開終端,到工程目錄中峦失, 輸入:
grep -r advertisingIdentifier .
可以看到那些文件中用到了IDFA扇丛,如果用到了就會被顯示出來
11.動(dòng)態(tài)計(jì)算文本高度
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
CGSize size = [@"相關(guān)NSString" boundingRectWithSize:CGSizeMake(100, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
12.關(guān)閉/收起鍵盤方法
1、點(diǎn)擊Return按扭時(shí)收起鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
2尉辑、點(diǎn)擊背景View收起鍵盤
[self.view endEditing:YES];
3帆精、你可以在任何地方加上這句話,可以用來統(tǒng)一收起鍵盤
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
13.修改textField的placeholder的字體顏色、大小
//方法1
self.textField.placeholder = @"username is in here!";
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//方法二(實(shí)際項(xiàng)目中使用過)
NSString *string = @"美麗新世界";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, [string length])];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16]
range:NSMakeRange(0, [string length])];
self.textField.attributedPlaceholder = attributedString;
14.待發(fā)現(xiàn)
...
15.待發(fā)現(xiàn)
...
16.待發(fā)現(xiàn)
...
ps:第一次寫沒有什么經(jīng)驗(yàn)就寫一點(diǎn)基礎(chǔ)簡單的吧卓练,我分享的這些都是經(jīng)過自己驗(yàn)證過的隘蝎,大家可以放心使用
參考文章1
參考文章2