iOS 小技巧

1. 打印View所有子視圖

[[self view]recursiveDescription]

2. NSString過濾特殊字符

// 定義一個(gè)特殊字符的集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:;()¥「」"搀罢、[]{}#%-*+=_\\|~<>$€^?'@#$%^&*()_+'\""];
// 過濾字符串的特殊字符
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];

3. TransForm屬性

//平移按鈕
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformTranslate(transForm, 10, 0);
 
//旋轉(zhuǎn)按鈕
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4);
 
//縮放按鈕
self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2);
 
//初始化復(fù)位
self.buttonView.transform = CGAffineTransformIdentity;

4. 去掉分割線多余15像素

首先在viewDidLoad方法加入以下代碼:
 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];    
}   
 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然后在重寫willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath{   
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {       
             [cell setSeparatorInset:UIEdgeInsetsZero];    
    }    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {        
             [cell setLayoutMargins:UIEdgeInsetsZero];    
    }
}

5. 計(jì)算方法耗時(shí)時(shí)間間隔

// 獲取時(shí)間間隔
#define TICK   CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

6. Color顏色宏定義

// 隨機(jī)顏色
#define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// 顏色(RGB)
#define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
// 利用這種方法設(shè)置顏色和透明值蝗岖,可不影響子視圖背景色
#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

7. Alert提示宏定義

#define Alert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil] show]

8. 讓iOS應(yīng)用直接退出

- (void)exitApplication {
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
 
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
    } completion:^(BOOL finished) {
        exit(0);
    }];
}

9. NSArray 快速求總和 最大值 最小值 和 平均值

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",sum,avg,max,min);

10. 修改Label中不同文字顏色

- (void)touchesEnded:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event
{
    [self editStringColor:self.label.text editStr:@"好" color:[UIColor blueColor]];
}
 
- (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
    // string為整體字符串, editStr為需要修改的字符串
    NSRange range = [string rangeOfString:editStr];
 
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
 
    // 設(shè)置屬性修改字體顏色UIColor與大小UIFont
    [attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];
 
    self.label.attributedText = attribute;
}

11. 播放聲音

#import<AVFoundation>
 //  1.獲取音效資源的路徑
 NSString *path = [[NSBundle mainBundle]pathForResource:@"pour_milk" ofType:@"wav"];
 //  2.將路勁轉(zhuǎn)化為url
 NSURL *tempUrl = [NSURL fileURLWithPath:path];
 //  3.用轉(zhuǎn)化成的url創(chuàng)建一個(gè)播放器
 NSError *error = nil;
 AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:tempUrl error:&error];
 self.player = play;
 //  4.播放
 [play play];

12. 檢測(cè)是否IPad Pro

- (BOOL)isIpadPro
{   
  UIScreen *Screen = [UIScreen mainScreen];   
  CGFloat width = Screen.nativeBounds.size.width/Screen.nativeScale;  
  CGFloat height = Screen.nativeBounds.size.height/Screen.nativeScale;         
  BOOL isIpad =[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;   
  BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>> ~/.lldbinit
    echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit
下次重新運(yùn)行項(xiàng)目,然后就不報(bào)錯(cuò)了榔至。

13. Label行間距

-(void)test{
    NSMutableAttributedString *attributedString =    
   [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
    NSMutableParagraphStyle *paragraphStyle =  [[NSMutableParagraphStyle alloc] init];  
   [paragraphStyle setLineSpacing:3];
 
    //調(diào)整行間距       
   [attributedString addAttribute:NSParagraphStyleAttributeName
                         value:paragraphStyle 
                         range:NSMakeRange(0, [self.contentLabel.text length])];
     self.contentLabel.attributedText = attributedString;
}

14. UIImageView填充模式

@"UIViewContentModeScaleToFill",      // 拉伸自適應(yīng)填滿整個(gè)視圖  
@"UIViewContentModeScaleAspectFit",   // 自適應(yīng)比例大小顯示  
@"UIViewContentModeScaleAspectFill",  // 原始大小顯示  
@"UIViewContentModeRedraw",           // 尺寸改變時(shí)重繪  
@"UIViewContentModeCenter",           // 中間  
@"UIViewContentModeTop",              // 頂部  
@"UIViewContentModeBottom",           // 底部  
@"UIViewContentModeLeft",             // 中間貼左  
@"UIViewContentModeRight",            // 中間貼右  
@"UIViewContentModeTopLeft",          // 貼左上  
@"UIViewContentModeTopRight",         // 貼右上  
@"UIViewContentModeBottomLeft",       // 貼左下  
@"UIViewContentModeBottomRight",      // 貼右下

15. 宏定義檢測(cè)block是否可用

#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };   
// 宏定義之前的用法
 if (completionBlock)   {   
    completionBlock(arg1, arg2); 
  }    
// 宏定義之后的用法
 BLOCK_EXEC(completionBlock, arg1, arg2);

16. Debug欄打印時(shí)自動(dòng)把Unicode編碼轉(zhuǎn)化成漢字

// 有時(shí)候我們?cè)趚code中打印中文,會(huì)打印出Unicode編碼,還需要自己去一些在線網(wǎng)站轉(zhuǎn)換,有了插件就方便多了抵赢。
 DXXcodeConsoleUnicodePlugin 插件

17. 設(shè)置狀態(tài)欄文字樣式顏色

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

18. 自動(dòng)生成模型代碼的插件

// 可自動(dòng)生成模型的代碼,省去寫模型代碼的時(shí)間
ESJsonFormat-for-Xcode

19. iOS中的一些手勢(shì)

輕擊手勢(shì)(TapGestureRecognizer)
輕掃手勢(shì)(SwipeGestureRecognizer)
長(zhǎng)按手勢(shì)(LongPressGestureRecognizer)
拖動(dòng)手勢(shì)(PanGestureRecognizer)
捏合手勢(shì)(PinchGestureRecognizer)
旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer)

20. iOS 開發(fā)中一些相關(guān)的路徑

模擬器的位置:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs 
 
文檔安裝位置:
/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
 
插件保存路徑:
~/Library/ApplicationSupport/Developer/Shared/Xcode/Plug-ins
 
自定義代碼段的保存路徑:
~/Library/Developer/Xcode/UserData/CodeSnippets/ 
如果找不到CodeSnippets文件夾唧取,可以自己新建一個(gè)CodeSnippets文件夾铅鲤。
 
證書路徑
~/Library/MobileDevice/Provisioning Profiles

21. 獲取 iOS 路徑的方法

獲取家目錄路徑的函數(shù)
NSString *homeDir = NSHomeDirectory();
 
獲取Documents目錄路徑的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
 
獲取Documents目錄路徑的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
 
獲取tmp目錄路徑的方法:
NSString *tmpDir = NSTemporaryDirectory();

22. 字符串相關(guān)操作

去除所有的空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""]
 
去除首尾的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 
- (NSString *)uppercaseString; 全部字符轉(zhuǎn)為大寫字母
- (NSString *)lowercaseString 全部字符轉(zhuǎn)為小寫字母

23. 把tableview里cell的小對(duì)勾的顏色改成別的顏色

_mTableView.tintColor = [UIColor redColor];

24. 調(diào)整tableview的separaLine線的位置

tableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);

25. 設(shè)置滑動(dòng)的時(shí)候隱藏navigationbar

navigationController.hidesBarsOnSwipe = Yes

26. Quartz2D相關(guān)

圖形上下是一個(gè)CGContextRef類型的數(shù)據(jù)。
圖形上下文包含:
1兵怯,繪圖路徑(各種各樣圖形)
2彩匕,繪圖狀態(tài)(顏色,線寬媒区,樣式驼仪,旋轉(zhuǎn)掸犬,縮放,平移)
3绪爸,輸出目標(biāo)(繪制到什么地方去湾碎?UIView、圖片)
 
1奠货,獲取當(dāng)前圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
2介褥,添加線條
CGContextMoveToPoint(ctx, 20, 20);
3,渲染
CGContextStrokePath(ctx);
CGContextFillPath(ctx);
4递惋,關(guān)閉路徑
CGContextClosePath(ctx);
5柔滔,畫矩形
CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));
6,設(shè)置線條顏色
[[UIColor redColor] setStroke];
7萍虽, 設(shè)置線條寬度
CGContextSetLineWidth(ctx, 20);
8睛廊,設(shè)置頭尾樣式
CGContextSetLineCap(ctx, kCGLineCapSquare);
9,設(shè)置轉(zhuǎn)折點(diǎn)樣式
CGContextSetLineJoin(ctx, kCGLineJoinBevel);
10杉编,畫圓
CGContextAddEllipseInRect(ctx, CGRectMake(30, 50, 100, 100));
11超全,指定圓心
CGContextAddArc(ctx, 100, 100, 50, 0, M_PI * 2, 1);
12,獲取圖片上下文
UIGraphicsGetImageFromCurrentImageContext();
13邓馒,保存圖形上下文
CGContextSaveGState(ctx)
14嘶朱,恢復(fù)圖形上下文
CGContextRestoreGState(ctx)

27. 屏幕截圖

// 1. 開啟一個(gè)與圖片相關(guān)的圖形上下文
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0.0);
 
    // 2. 獲取當(dāng)前圖形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
 
    // 3. 獲取需要截取的view的layer
    [self.view.layer renderInContext:ctx];
 
    // 4. 從當(dāng)前上下文中獲取圖片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 
    // 5. 關(guān)閉圖形上下文
    UIGraphicsEndImageContext();
 
    // 6. 把圖片保存到相冊(cè)
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

28. 隱藏導(dǎo)航欄上的返回字體

//Swift
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics: .Default)
//OC
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

29. 解決tableview的分割線短一截

-(void)viewDidLayoutSubviews{
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
{ 
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) 
{
[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; 
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) 
{
[cell setSeparatorInset:UIEdgeInsetsZero]; 
} 
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) 
{
[cell setLayoutMargins:UIEdgeInsetsZero]; 
}
}

30. 動(dòng)態(tài)隱藏NavigationBar

//1.當(dāng)我們的手離開屏幕時(shí)候隱藏
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{ 
if(velocity.y > 0) 
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES]; 
}
}
velocity.y這個(gè)量,在上滑和下滑時(shí)光酣,變化極惺瓒簟(小數(shù)),但是因?yàn)榉较虿煌医姓?fù)之分改览,這就很好處理了下翎。
//2.在滑動(dòng)過程中隱藏
//像safari
(1) 
self.navigationController.hidesBarsOnSwipe = YES;
(2)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 CGFloat offsetY = scrollView.contentOffset.y + __tableView.contentInset.top;
 CGFloat panTranslationY = [scrollView.panGestureRecognizer translationInView:self.tableView].y;
 if (offsetY > 64) {
 if (panTranslationY > 0) 
{ 
//下滑趨勢(shì)缤言,顯示 
[self.navigationController setNavigationBarHidden:NO animated:YES];
} else { 
//上滑趨勢(shì),隱藏 
[self.navigationController setNavigationBarHidden:YES animated:YES]; 
}
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES]; 
}
}
這里的offsetY > 64只是為了在視圖滑過navigationBar的高度之后才開始處理视事,防止影響展示效果胆萧。panTranslationY是scrollView的pan手勢(shì)的手指位置的y值,可能不是太好俐东,因?yàn)閜anTranslationY這個(gè)值在較小幅度上下滑動(dòng)時(shí)切心,可能都為正或都為負(fù)垂券,這就使得這一方式不太靈敏.

31. 設(shè)置導(dǎo)航欄透明

//方法一:設(shè)置透明度
[[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:0.1];
//方法二:設(shè)置背景圖片
/**
 * 設(shè)置導(dǎo)航欄,使其透明
 *
*/
- (void)setNavigationBarColor:(UIColor *)color targetController:(UIViewController *)targetViewController{
//導(dǎo)航條的顏色 以及隱藏導(dǎo)航條的顏色targetViewController.navigationController.navigationBar.shadowImage = [[UIImage alloc]init]; 
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(); [targetViewController.navigationController.navigationBar setBackgroundImage:theImage forBarMetrics:UIBarMetricsDefault];
}

32. 設(shè)置字體和行間距

//設(shè)置字體和行間距 
UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 200)]; 
lable.text = @"大家好,我是Frank_chun,在這里我們一起學(xué)習(xí)新的知識(shí),總結(jié)我們遇到的那些坑,共同的學(xué)習(xí),共同的進(jìn)步,共同的努力,只為美好的明天!!!有問題一起相互的探討--438637472!!!"; 
lable.numberOfLines = 0;
lable.font = [UIFont systemFontOfSize:12];
lable.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:lable]; 
//設(shè)置每個(gè)字體之間的間距 
//NSKernAttributeName 這個(gè)對(duì)象所對(duì)應(yīng)的值是一個(gè)NSNumber對(duì)象(包含小數(shù)),作用是修改默認(rèn)字體之間的距離調(diào)整,值為0的話表示字距調(diào)整是禁用的; NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:lable.text attributes:@{NSKernAttributeName:@(5.0)}];
//設(shè)置某寫字體的顏色
//NSForegroundColorAttributeName 設(shè)置字體顏色
NSRange blueRange = NSMakeRange([[str string] rangeOfString:@"Frank_chun"].location, [[str string] rangeOfString:@"Frank_chun"].length); 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:blueRange]; 
NSRange blueRange1 = NSMakeRange([[str string] rangeOfString:@"438637472"].location, [[str string] rangeOfString:@"438637472"].length);
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:blueRange1];
//設(shè)置每行之間的間距 
//NSParagraphStyleAttributeName 設(shè)置段落的樣式
NSMutableParagraphStyle * par = [[NSMutableParagraphStyle alloc]init];
[par setLineSpacing:20];
//為某一范圍內(nèi)文字添加某個(gè)屬性
//NSMakeRange表示所要的范圍,從0到整個(gè)文本的長(zhǎng)度
[str addAttribute:NSParagraphStyleAttributeName value:par range:NSMakeRange(0, lable.text.length)]; [lable setAttributedText:str];

33. 點(diǎn)擊button倒計(jì)時(shí)

#pragma mark -點(diǎn)擊發(fā)送驗(yàn)證碼
- (void)sendMessage:(UIButton *)btn{
if (self.phoneField.text.length == 0) { 
[self remindMessage:@"請(qǐng)輸入正確的手機(jī)號(hào)"];
}else{ 
__block int timeout=60; 
//倒計(jì)時(shí)時(shí)間 
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
 //每秒執(zhí)行 
dispatch_source_set_event_handler(_timer, ^{ 
if(timeout<=0){ 
//倒計(jì)時(shí)結(jié)束,關(guān)閉 
dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ 
// 設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置 
[btn setTitle:@"發(fā)送驗(yàn)證碼" forState:UIControlStateNormal]; btn.userInteractionEnabled = YES; 
}); 
}else{ 
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
if ([strTime isEqualToString:@"0"]) {
 strTime = [NSString stringWithFormat:@"%d",60];
 } 
dispatch_async(dispatch_get_main_queue(), ^{ 
//設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置 
//NSLog(@"____%@",strTime);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1]; 
[btn setTitle:[NSString stringWithFormat:@"%@秒后重新發(fā)送",strTime] forState:UIControlStateNormal];
[UIView commitAnimations]; 
btn.userInteractionEnabled = NO;
 });
 timeout--;
 } 
}); 
dispatch_resume(_timer);
}

34. UITextField默認(rèn)占位符是居中顯示,讓其居上顯示

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

35. 解決同時(shí)按兩個(gè)按鈕進(jìn)兩個(gè)view的問題

[button setExclusiveTouch:YES];

36. 圖片拉伸

UIImage* img=[UIImage imageNamed:@"2.png"];//原圖
UIEdgeInsets edge=UIEdgeInsetsMake(0, 10, 0,10);
//UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片
//UIImageResizingModeTile:平鋪模式弦蹂,通過重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來填充圖
img= [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
self.imageView.image=img;

37. 修改textFieldplaceholder字體顏色和大小

textField.placeholder = @"請(qǐng)輸入手機(jī)號(hào)碼";
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];

38. 修改狀態(tài)欄字體顏色

只能設(shè)置兩種顏色,黑色和白色封拧,系統(tǒng)默認(rèn)黑色
設(shè)置為白色方法:
(1)在plist里面添加Status bar style复唤,值為UIStatusBarStyleLightContent(白色)或UIStatusBarStyleDefault(黑 色)
(2)在Info.plist中設(shè)置UIViewControllerBasedStatusBarAppearance 為NO

39. 去掉導(dǎo)航欄下邊的黑線

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

40. 修改pagecontrol顏色

_pageControl.currentPageIndicatorTintColor=SFQRedColor;
_pageControl.pageIndicatorTintColor=SFQGrayColor;

41. 去掉UITableView的section的粘性奕枢,使其不會(huì)懸停

//有時(shí)候使用UITableView所實(shí)現(xiàn)的列表,會(huì)使用到section佩微,但是又不希望它粘在最頂上而是跟隨滾動(dòng)而消失或者出現(xiàn)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {  
    if (scrollView == _tableView) {  
        CGFloat sectionHeaderHeight = 36;
 
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {  
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);  
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {  
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);  
        }  
    }  
}

42. 通過2D仿射函數(shù)實(shí)現(xiàn)小的動(dòng)畫效果(變大縮小) --可用于自定義pageControl中

[UIView animateWithDuration:0.3 animations:^{
       imageView.transform = CGAffineTransformMakeScale(2, 2);
       } completion:^(BOOL finished) {
       imageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
       }];

43. UIImage與字符串互轉(zhuǎn)

//圖片轉(zhuǎn)字符串  
-(NSString *)UIImageToBase64Str:(UIImage *) image  
{  
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);  
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];  
    return encodedImageStr;  
}
 
//字符串轉(zhuǎn)圖片  
-(UIImage *)Base64StrToUIImage:(NSString *)_encodedImageStr  
{  
    NSData *_decodedImageData   = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];  
    UIImage *_decodedImage      = [UIImage imageWithData:_decodedImageData];  
    return _decodedImage;  
}

44. 判斷NSString中是否包含中文

-(BOOL)isChinese:(NSString *)str{
    NSString *match=@"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:str];
}

45. NSDate與NSString的相互轉(zhuǎn)化

-(NSString *)dateToString:(NSDate *)date {
  // 初始化時(shí)間格式控制器
  NSDateFormatter *matter = [[NSDateFormatter alloc] init];
  // 設(shè)置設(shè)計(jì)格式
  [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
  // 進(jìn)行轉(zhuǎn)換
  NSString *dateStr = [matter stringFromDate:date];
  return dateStr;
}
-(NSDate *)stringToDate:(NSString *)dateStr {
 
  // 初始化時(shí)間格式控制器
  NSDateFormatter *matter = [[NSDateFormatter alloc] init];
  // 設(shè)置設(shè)計(jì)格式
  [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
  // 進(jìn)行轉(zhuǎn)換
  NSDate *date = [matter dateFromString:dateStr];
  return date;
}

46. UITextField 小技巧

//設(shè)置UITextField的placeholder的顏色
    [_phoneTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
//設(shè)置UITextField的placeholder的字號(hào)
    [_phoneTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//設(shè)置UITextField的placeholder的左邊距
_phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    _phoneTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缝彬,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子哺眯,更是在濱河造成了極大的恐慌谷浅,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,126評(píng)論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奶卓,死亡現(xiàn)場(chǎng)離奇詭異一疯,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)夺姑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門违施,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瑟幕,你說我怎么就攤上這事磕蒲。” “怎么了只盹?”我有些...
    開封第一講書人閱讀 169,941評(píng)論 0 366
  • 文/不壞的土叔 我叫張陵辣往,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我殖卑,道長(zhǎng)站削,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,294評(píng)論 1 300
  • 正文 為了忘掉前任孵稽,我火速辦了婚禮许起,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘菩鲜。我一直安慰自己园细,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評(píng)論 6 398
  • 文/花漫 我一把揭開白布接校。 她就那樣靜靜地躺著猛频,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蛛勉。 梳的紋絲不亂的頭發(fā)上鹿寻,一...
    開封第一講書人閱讀 52,874評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音诽凌,去河邊找鬼毡熏。 笑死,一個(gè)胖子當(dāng)著我的面吹牛侣诵,可吹牛的內(nèi)容都是我干的痢法。 我是一名探鬼主播恬试,決...
    沈念sama閱讀 41,285評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼疯暑!你這毒婦竟也來了训柴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤妇拯,失蹤者是張志新(化名)和其女友劉穎幻馁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體越锈,經(jīng)...
    沈念sama閱讀 46,760評(píng)論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡仗嗦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評(píng)論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了甘凭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片稀拐。...
    茶點(diǎn)故事閱讀 40,973評(píng)論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖丹弱,靈堂內(nèi)的尸體忽然破棺而出德撬,到底是詐尸還是另有隱情,我是刑警寧澤躲胳,帶...
    沈念sama閱讀 36,631評(píng)論 5 351
  • 正文 年R本政府宣布蜓洪,位于F島的核電站,受9級(jí)特大地震影響坯苹,放射性物質(zhì)發(fā)生泄漏隆檀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評(píng)論 3 336
  • 文/蒙蒙 一粹湃、第九天 我趴在偏房一處隱蔽的房頂上張望恐仑。 院中可真熱鬧,春花似錦为鳄、人聲如沸裳仆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鉴逞。三九已至,卻和暖如春司训,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背液南。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評(píng)論 1 275
  • 我被黑心中介騙來泰國(guó)打工壳猜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人滑凉。 一個(gè)月前我還...
    沈念sama閱讀 49,431評(píng)論 3 379
  • 正文 我出身青樓统扳,卻偏偏與公主長(zhǎng)得像喘帚,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子咒钟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 原文 在這里總結(jié)一些iOS開發(fā)中的小技巧吹由,能大大方便我們的開發(fā),持續(xù)更新朱嘴。 1.UITableView的Group...
    無灃閱讀 782評(píng)論 0 2
  • 在這里總結(jié)一些iOS開發(fā)中的小技巧倾鲫,能大大方便我們的開發(fā) 原文地址:http://www.reibang.com/...
    Marray閱讀 334評(píng)論 0 0
  • 在這里總結(jié)一些iOS開發(fā)中的小技巧,能大大方便我們的開發(fā)萍嬉,持續(xù)更新乌昔。 UITableView的Group樣式下頂部...
    UI愛好者閱讀 521評(píng)論 0 0
  • UITableView的Group樣式下頂部空白處理分組列表頭部空白處理UIView*view = [[UIVie...
    沒技術(shù)的BUG開發(fā)攻城獅閱讀 666評(píng)論 0 0
  • 焦點(diǎn)網(wǎng)絡(luò)初級(jí) 程玲玲 鄭州 堅(jiān)持分享第90天 每一個(gè)憤怒磕道,悲傷,恐懼的情緒后面行冰,都是缺乏安全感溺蕉,沒有得到肯定,認(rèn)可...
    思小念052閱讀 127評(píng)論 0 0