iOS開發(fā)零碎知識(shí)點(diǎn)

本篇文章記錄了iOS開發(fā)零碎知識(shí)點(diǎn),簡(jiǎn)單又實(shí)用!

代碼寫了這么多,但是總是有些知識(shí)點(diǎn)在真正需要用到的時(shí)候卻遺忘了,一直想整理這塊知識(shí),最近又總是在趕項(xiàng)目,不管再忙,這塊總是要整理起來。


iOS開發(fā)零碎知識(shí)點(diǎn)

修改Cell分割線距離

修改UITableviewCell的分割線距離通常需要修改separatorInset屬性的top, left, bottom, right:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

? ? [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];

}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

? ? [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];

}

if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {

? ? [cell setPreservesSuperviewLayoutMargins:NO];

}

}

去掉Cell的分割線

myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

取消Cell的選中效果

myTableView.selectionStyle = UITableViewCellSelectionStyleNone;

將漢字轉(zhuǎn)換為拼音

可以把漢字字符串轉(zhuǎn)換成拼音,并且支持是否在拼音間插入空格

- (NSString*)chineseToPinyin:(NSString*)chinese withSpace:(BOOL)withSpace {

? ? if(chinese) {

? ? ? ? CFStringRefhanzi = (__bridgeCFStringRef)chinese;

? ? ? ? CFMutableStringRefstring =CFStringCreateMutableCopy(NULL,0, hanzi);

? ? ? ? CFStringTransform(string,NULL, kCFStringTransformMandarinLatin,NO);

? ? ? ? CFStringTransform(string,NULL, kCFStringTransformStripDiacritics,NO);

? ? ? ? NSString*pinyin = (NSString*)CFBridgingRelease(string);

? ? ? ? if(!withSpace) {

? ? ? ? ? ? pinyin = [pinyin stringByReplacingOccurrencesOfString:@" "withString:@""];

? ? ? ? }

? ? return pinyin;

? ? }

return nil;

}

重置self.navigationController.viewControllers

NSArray*vcs = self.navigationController.viewControllers;

NSMutableArray*array = [NSMutableArrayarray];

for (inti =0; i < vcs.count; i++) {

UIViewController*temp = [vcsobjectAtIndex:i];

if (![tempisKindOfClass:NSClassFromString(viewControllersName)]) {

[arrayaddObject:temp];

}

}

[self.navigationControllersetViewControllers:arrayanimated:YES];

擴(kuò)大UIButton點(diǎn)擊區(qū)域

當(dāng)UI設(shè)計(jì)圖上的給出按鈕尺寸較小,我們將對(duì)應(yīng)的資源文件放入U(xiǎn)IButton中,在真機(jī)調(diào)試中會(huì)發(fā)現(xiàn)難以點(diǎn)到按鈕。這時(shí)候可以通過繼承UIButton,重寫pointInside方法,使得按鈕事件響應(yīng)不夠我們?cè)O(shè)置的最小區(qū)域的自動(dòng)擴(kuò)大到我們的設(shè)置的最小區(qū)域。

.h定義我們?cè)O(shè)置的最小響應(yīng)區(qū)域大小
/**

*? 事件響應(yīng)最小區(qū)域大小(小于此區(qū)域則放大,否則保持原大小不變,不賦值保持原大小不變)

*/

@property(nonatomic,assign)CGSizeeventFrame;

.m重寫pointInside方法

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event{

CGRectbounds =self.bounds;

CGFloatwidthExtra = MAX(self.eventFrame.width- bounds.size.width,0);

CGFloatheightExtra = MAX(self.eventFrame.width- bounds.size.height,0);

bounds =CGRectInset(bounds, -0.5* widthExtra, -0.5* heightExtra);

returnCGRectContainsPoint(bounds, point);

}

判斷非空字符串

+ (BOOL)isEmptyString:(NSString *)string {

if (string == nil || string == NULL) {

return YES;

}

if ([string isKindOfClass:[NSNull class]]) {

return YES;

}

if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {

return YES;

}

return NO;

}

設(shè)置抗壓縮-抗拉伸

[self.textFiledsetContentHuggingPriority:UILayoutPriorityDefaultLowforAxis:UILayoutConstraintAxisHorizontal];[self.textFiledsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentHuggingPriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];

首次進(jìn)入某一功能模塊判斷

+ (BOOL)isFirstEnterNewModule{

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]) {

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];

[[NSUserDefaults standardUserDefaults] synchronize];

return YES;

}

return NO;

}

視圖過大不響應(yīng)

子視圖超出父視圖,子視圖點(diǎn)擊事件不響應(yīng)。一般子視圖超出父視圖,子視圖點(diǎn)擊等事件是不響應(yīng)的,因?yàn)槭录膫鬟f鏈不會(huì)傳到超出父視圖的視圖上面,需要我們用``hitTest:withEvent:``處理下。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

? ? CGPoint hitPoint = [self.cardView.dayRateHelp convertPoint:point fromView:self];

? ? if ([self.cardView.dayRateHelp pointInside:hitPoint withEvent:event])

? ? return self.cardView.dayRateHelp;

? ? return [super hitTest:point withEvent:event];

}

注意:如果父視圖是UIScrollView,需要設(shè)置`self.bgScrollView.clipsToBounds = NO;`,因?yàn)閌UIScrollView`默認(rèn)會(huì)進(jìn)行裁剪,會(huì)導(dǎo)致超出的部分沒有了。

修改holder

修改UITextField的Placeholder的文字顏色和大小。這里我們使用kvc設(shè)置UITextField的私有屬性。

[textField setValue:placeholderLabelTextColor forKeyPath:@"_placeholderLabel.textColor"];[textField setValue:[UIFont systemFontOfSize:placeholderLabelFont] forKeyPath:@"_placeholderLabel.font"];

修改UIPageControl圖片

修改UIPageControl的選中圖片和默認(rèn)圖片。默認(rèn)也是不允許修改的,需要用到kvc設(shè)置。

[self.pageControl setValue:currentImage forKey:@"_currentPageImage"];

[self.pageControl setValue:pageImage forKey:@"_pageImage"];

打電話

NSString *phoneNum = @"";

NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];

if ( !phoneCallWebView ) {

? ? phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];

}

[phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];

修改系統(tǒng)相機(jī)拍照功能

1、將使用照片改成保存至相冊(cè);

2、監(jiān)聽拍照按鈕點(diǎn)擊事件;

3、監(jiān)聽重拍按鈕點(diǎn)擊事件;

4、在拍照里面添加自定義view放到cameraOverlayView上。

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

[self addSomeElements:viewController];

}

- (UIView *)findView:(UIView *)aView withName:(NSString *)name {

? ? Class cl = [aView class];

? ? NSString *desc = [cl description];

? ? if ([name isEqualToString:desc]) return aView;

? ? for (UIView *view in aView.subviews) {

? ? ? ? Class cll = [view class];

? ? ? ? NSString *stringl = [cll description];

? ? ? ? if ([stringl isEqualToString:name]) {

? ? ? ? ? ? return view;

? ? ? ? }

? ? }

? ? return nil;

}

- (void)addSomeElements:(UIViewController *)viewController {

? ? UIView *PLCropOverlay = [self findView:viewController.view withName:@"PLCropOverlay"];

? ? UIView *PLCropOverlayBottomBar = [self findView:PLCropOverlay withName:@"PLCropOverlayBottomBar"];

? ? UIView *PLCropOverlayPreviewBottomBar = [self findView:PLCropOverlayBottomBar withName:@"PLCropOverlayPreviewBottomBar"];

? ? UIButton *userButton = [PLCropOverlayPreviewBottomBar.subviews objectAtIndex:2];

? ? UIButton *viewbtn = [[UIButton alloc] init];

? ? [viewbtn setTitle:@"保存至相冊(cè)" forState:UIControlStateNormal];

? ? [viewbtn setTitleColor:[UIColor whiteColor] forState:0];

? ? viewbtn.backgroundColor = RGB(19, 20, 21);

? ? [userButton addSubview:viewbtn];

? ? [viewbtn mas_makeConstraints:^(MASConstraintMaker *make) {

? ? ? ? make.trailing.equalTo(userButton.mas_trailing);

? ? ? ? make.centerY.equalTo(userButton);

}];

viewbtn.userInteractionEnabled = NO;

//給拍照加點(diǎn)擊事件

UIView *CMKBottomBar = [self findView:viewController.view withName:@"CMKBottomBar"];

UIButton *CMKShutterButton = (UIButton *) [self findView:CMKBottomBar withName:@"CMKShutterButton"];

[CMKShutterButton addTarget:self action:@selector(shutterButtonClicked) forControlEvents:UIControlEventTouchUpInside];

//監(jiān)聽重拍

UIButton *resetButton = [PLCropOverlayPreviewBottomBar.subviews objectAtIndex:0];

[resetButton addTarget:self action:@selector(resetButtonClicked) forControlEvents:UIControlEventTouchUpInside];

}

注意:viewbtn.userInteractionEnabled = NO;的作用是防止這層視圖的點(diǎn)擊事件影響系統(tǒng)的使用照片按鈕的點(diǎn)擊事件;在這里給拍照按鈕和重拍按鈕添加了點(diǎn)擊事件,既滿足了自己需要做的事情,又不影響系統(tǒng)對(duì)這兩個(gè)按鈕的點(diǎn)擊事件。

iOS10 UIPickerView線條不顯示

#define IOS10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >=10.0)

if (IOS10_OR_LATER) {

? ? for (UIView*separatorLine in pickerView.subviews) {

? ? ? ? if (separatorLine.frame.size.height<1) {

? ? ? ? separatorLine.backgroundColor= [UIColorwd_colorWithd0d0d0];

? ? ? ? }

? ? }

}

持續(xù)更新中。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評(píng)論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,582評(píng)論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,540評(píng)論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,801評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,223評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,442評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,976評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,800評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,996評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評(píng)論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,233評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評(píng)論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,702評(píng)論 3 392
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,991評(píng)論 2 374

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

  • 引自:http://m.blog.csdn.net/article/details?id=52180380 記錄一...
    雪_晟閱讀 346評(píng)論 0 0
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,512評(píng)論 1 14
  • 現(xiàn)在是二零一七年九月二十八日,周四,雙節(jié)將至。我是在前行的火車上,可是我并不是回家,也不是去學(xué)校。 我卻是從一家科...
    燕研小視界閱讀 415評(píng)論 2 1
  • 午間美好生活提案 職場(chǎng)人的小成本逼格 一直在說斷舍離。提到這三個(gè)字,你腦海中可能早已浮現(xiàn)出一些概念甚或畫面。 比...
    午休在別處閱讀 423評(píng)論 0 0
  • 20161206,今早跟孩子溝通,我說:我們說別人好就是說自己好,以后媽媽就說你好,不說你不好。因?yàn)檎f別人就是說自...
    銘瑋閱讀 270評(píng)論 0 0