【IOS】IOS開發(fā)總結(jié),持續(xù)更新...

  1. 給一個view截圖

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

2. collectionView的內(nèi)容小于其寬高的時候是不能滾動的谣蠢,設(shè)置可以滾動:

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

3. 設(shè)置navigationBar上的title顏色和大小

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

4.  顏色轉(zhuǎn)圖片

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義緩存大小
//用枚舉器遍歷 一個文件夾的內(nèi)容
//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;//自定義所有緩存大小
}
// 得到是字節(jié)  轉(zhuǎn)化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

5. 獲取APP緩存大小

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義緩存大小
//用枚舉器遍歷 一個文件夾的內(nèi)容
//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;//自定義所有緩存大小
}
// 得到是字節(jié)  轉(zhuǎn)化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

6. 清理APP緩存
- (void)handleClearView {
 //刪除兩部分
 //1.刪除 sd 圖片緩存
 //先清除內(nèi)存中的圖片緩存
 [[SDImageCache sharedImageCache] clearMemory];
 //清除磁盤的緩存
 [[SDImageCache sharedImageCache] clearDisk];
 //2.刪除自己緩存
 NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
 [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
 }```
  1. 身份證號驗(yàn)證
    - (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實(shí)現(xiàn)placeHolder占位文字
    通過runtime,我們發(fā)現(xiàn)查近,UITextView內(nèi)部有一個名為“_placeHolderLabel”的私有成員變量眉踱。大家知道,Objective-C沒有絕對的私有變量霜威,因?yàn)槲覀兛梢酝ㄟ^KVC來訪問私有變量谈喳。
    特點(diǎn):雖然Apple官方?jīng)]有給我們開發(fā)者提供類似于placeholder的屬性,但是通過運(yùn)行時戈泼,我們遍歷出了一個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互轉(zhuǎn)

// 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設(shè)置為空字符串

  • 去掉icon上的角標(biāo)數(shù)字

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末淀零,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子膛壹,更是在濱河造成了極大的恐慌驾中,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件模聋,死亡現(xiàn)場離奇詭異肩民,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)链方,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門持痰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人侄柔,你說我怎么就攤上這事共啃。” “怎么了暂题?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵移剪,是天一觀的道長。 經(jīng)常有香客問我薪者,道長纵苛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任言津,我火速辦了婚禮攻人,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘悬槽。我一直安慰自己怀吻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布初婆。 她就那樣靜靜地躺著蓬坡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪磅叛。 梳的紋絲不亂的頭發(fā)上屑咳,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音弊琴,去河邊找鬼兆龙。 笑死,一個胖子當(dāng)著我的面吹牛敲董,可吹牛的內(nèi)容都是我干的紫皇。 我是一名探鬼主播慰安,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坝橡!你這毒婦竟也來了泻帮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤计寇,失蹤者是張志新(化名)和其女友劉穎锣杂,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體番宁,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡元莫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蝶押。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片踱蠢。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖棋电,靈堂內(nèi)的尸體忽然破棺而出茎截,到底是詐尸還是另有隱情,我是刑警寧澤赶盔,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布企锌,位于F島的核電站,受9級特大地震影響于未,放射性物質(zhì)發(fā)生泄漏撕攒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一烘浦、第九天 我趴在偏房一處隱蔽的房頂上張望抖坪。 院中可真熱鬧,春花似錦闷叉、人聲如沸擦俐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捌肴。三九已至,卻和暖如春藕咏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背秽五。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工孽查, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人坦喘。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓盲再,卻偏偏與公主長得像西设,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子答朋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

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