1. 程式人生 > >【IOS】IOS開發總結,持續更新...

【IOS】IOS開發總結,持續更新...

  1. 給一個view截圖

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

2. collectionView的內容小於其寬高的時候是不能滾動的,設定可以滾動:

collectionView.alwaysBounceHorizontal = YES;
collectionView.alwaysBounceVertical = YES;

3. 設定navigationBar上的title顏色和大小

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor youColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}]

4.  顏色轉圖片

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義快取大小
//用列舉器遍歷 一個資料夾的內容
//1.獲取 資料夾列舉器
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
//2.遍歷
for (NSString *fileName in enumerator) {
    NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
    NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    count += fileDict.fileSize;//自定義所有快取大小
}
// 得到是位元組  轉化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

5. 獲取APP快取大小

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義快取大小
//用列舉器遍歷 一個資料夾的內容
//1.獲取 資料夾列舉器
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
//2.遍歷
for (NSString *fileName in enumerator) {
    NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
    NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    count += fileDict.fileSize;//自定義所有快取大小
}
// 得到是位元組  轉化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

6. 清理APP快取
- (void)handleClearView {
 //刪除兩部分
 //1.刪除 sd 圖片快取
 //先清除記憶體中的圖片快取
 [[SDImageCache sharedImageCache] clearMemory];
 //清除磁碟的快取
 [[SDImageCache sharedImageCache] clearDisk];
 //2.刪除自己快取
 NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
 [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
 }```
  1. 身份證號驗證
    - (BOOL)validateIdentityCard {
     BOOL flag;
     if (self.length <= 0) {
     flag = NO;
     return flag;
     }
     NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
     NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
     return [identityCardPredicate evaluateWithObject:self];
     }
  1. UITextView實現placeHolder佔位文字
    通過runtime,我們發現,UITextView內部有一個名為“_placeHolderLabel”的私有成員變數。大家知道,Objective-C沒有絕對的私有變數,因為我們可以通過KVC來訪問私有變數。
    特點:雖然Apple官方沒有給我們開發者提供類似於placeholder的屬性,但是通過執行時,我們遍歷出了一個placeHolderLabel的私有變數。這種方法簡單易懂,程式碼量少,推薦大家使用這種方法。
    UILabel *label =[JDUtils createLabelWithFrame:CGRectZero Font:18 Text:@"請輸入備註"];
    [label sizeToFit];
    label.textColor = [UIColor lightGrayColor];
    [_textView addSubview:label];
    [_textView setValue:label forKey:@"_placeholderLabel"];
    
    [self.view addSubview:_textView];
  • 刪除NSUserDefaults所有記錄
//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
//方法二   
 - (void)resetDefaults {
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defs dictionaryRepresentation];
    for (id key in dict) {
        [defs removeObjectForKey:key];
    }
    [defs synchronize];
}
// 方法三
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
  • 自定義cell選中背景顏色
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];```

- UIView背景顏色漸變
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
 [self.view addSubview:view];
 CAGradientLayer *gradient = [CAGradientLayer layer];
 gradient.frame = view.bounds;
 gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
 [view.layer insertSublayer:gradient atIndex:0];```
  • 在非ViewController的地方彈出UIAlertController對話方塊
//  最好抽成一個分類
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//...
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
    rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
    rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
}
[rootViewController presentViewController:alertController animated:YES completion:nil];```
- 獲取一個view所屬的控制器

// view分類方法

  • (UIViewController *)belongViewController {
    for (UIView next = [self superview]; next; next = next.superview) {
    UIResponder
    nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
    return (UIViewController *)nextResponder;
    }
    }
    return nil;
    }
- UIImage和base64互轉

// view分類方法

  • (NSString *)encodeToBase64String:(UIImage *)image {
    return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    }

  • (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
    NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
    }```

  • 讓推送只有聲音沒有橫幅的方法

    把本地推送的alertBody設定為空字串

  • 去掉icon上的角標數字

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];