iOS 開發(fā)小知識點(diǎn)

一、調(diào)用代碼使APP進(jìn)入后臺,達(dá)到點(diǎn)擊Home鍵的效果秀鞭。(私有API)

[[UIApplication sharedApplication] performSelector:@selector(suspend)];

suspend的英文意思:懸鸦列、掛逛尚、暫停


//tableview 如果沒有占滿則清除下面的

_tableviewKnowChrist.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

二吨些、獲取UIWebView的高度 個(gè)人

(void)webViewDidFinishLoad:(UIWebView *)webView? {? ? ? CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];? ? ? CGRect frame = webView.frame;? ? ? webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);? }

三搓谆、UITableView收起鍵盤何必這么麻煩 一個(gè)屬性搞定炒辉,效果好(UIScrollView同樣可以使用) 以前是不是覺得[self.view endEditing:YES];很屌豪墅,這個(gè)下面的更屌。

yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

四黔寇、設(shè)置UILable? 的行間距? 和 計(jì)算帶行間距的高度

/*

UILabel* 展示的控件

(NSString*)str? 內(nèi)容

withFont:(float)font 字體大小

WithSpace:(float)space 行間距

*/

//給UILabel設(shè)置行間距和字間距

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(float)font WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space; //設(shè)置行間距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//設(shè)置字間距 NSKernAttributeName:@1.5f

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];

label.attributedText = attributeStr;

}

/*

計(jì)算UILabel的高度(帶有行間距的情況)

(NSString*)str? 內(nèi)容

withFont:(float)font 字體大小

WithSpace:(float)space 行間距

(CGFloat)width? UILable的寬度

*/

//計(jì)算UILabel的高度(帶有行間距的情況)

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(float)font withWidth:(CGFloat)width WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

return size.height;

}

五偶器、 禁止程序運(yùn)行時(shí)自動鎖屏

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

六、CocoaPods pod install/pod update更新慢的問題

pod install –verbose –no-repo-update pod update –verbose –no-repo-update 如果不加后面的參數(shù)缝裤,默認(rèn)會升級CocoaPods的spec倉庫屏轰,加一個(gè)參數(shù)可以省略這一步,然后速度就會提升不少憋飞。

七霎苗、修改textFieldplaceholder字體顏色和大小

textField.placeholder = @"請輸入用戶名";? [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];? [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

八、禁止textField和textView的復(fù)制粘貼菜單

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{? ? if ([UIMenuController sharedMenuController]) {? ? ? [UIMenuController sharedMenuController].menuVisible = NO;? ? }? ? return NO;}

九榛做、級三級頁面隱藏系統(tǒng)tabbar 1唁盏、單個(gè)處理

YourViewController *yourVC = [YourViewController new];yourVC.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:yourVC animated:YES];

十、取消系統(tǒng)的返回手勢

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

十一检眯、百度坐標(biāo)跟火星坐標(biāo)相互轉(zhuǎn)換

//百度轉(zhuǎn)火星坐標(biāo)

+ (CLLocationCoordinate2D )bdToGGEncrypt:(CLLocationCoordinate2D)coord

{

double x = coord.longitude - 0.0065, y = coord.latitude - 0.006;

double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);

double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);

CLLocationCoordinate2D transformLocation ;

transformLocation.longitude = z * cos(theta);

transformLocation.latitude = z * sin(theta);

return transformLocation;

}

//火星坐標(biāo)轉(zhuǎn)百度坐標(biāo)

+ (CLLocationCoordinate2D )ggToBDEncrypt:(CLLocationCoordinate2D)coord

{

double x = coord.longitude, y = coord.latitude;

double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);

double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);

CLLocationCoordinate2D transformLocation ;

transformLocation.longitude = z * cos(theta) + 0.0065;

transformLocation.latitude = z * sin(theta) + 0.006;

return transformLocation;

}

十二厘擂、添加pch文件的步聚

1:創(chuàng)建新文件 ios->other->PCH file,創(chuàng)建一個(gè)pch文件:“工程名-Prefix.pch”:

2:將building setting中的precompile header選項(xiàng)的路徑添加“$(SRCROOT)/項(xiàng)目名稱/pch文件名”(例如:$(SRCROOT)/LotteryFive/LotteryFive-Prefix.pch)

3:將Precompile Prefix Header為YES锰瘸,預(yù)編譯后的pch文件會被緩存起來刽严,可以提高編譯速度

十三、關(guān)于Masonry

a:

make.equalTo 或 make.greaterThanOrEqualTo (至多) 或 make.lessThanOrEqualTo(至少)

make.left.greaterThanOrEqualTo(label);

make.left.greaterThanOrEqualTo(label.mas_left);

//width >= 200 && width <= 400

make.width.greaterThanOrEqualTo(@200);

make.width.lessThanOrEqualTo(@400)

b:masequalTo 和 equalTo 區(qū)別:masequalTo 比equalTo多了類型轉(zhuǎn)換操作避凝,一般來說舞萄,大多數(shù)時(shí)候兩個(gè)方法都是 通用的,但是對于數(shù)值元素使用mas_equalTo管削。對于對象或是多個(gè)屬性的處理鹏氧,使用equalTo。特別是多個(gè)屬性時(shí)佩谣,必須使用equalTo

c:一些簡便賦值

// make top = superview.top + 5, left = superview.left + 10,

// bottom = superview.bottom - 15, right = superview.right - 20

make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))

// make width and height greater than or equal to titleLabel

make.size.greaterThanOrEqualTo(titleLabel)

// make width = superview.width + 100, height = superview.height - 50

make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))

// make centerX = superview.centerX - 5, centerY = superview.centerY + 10

make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

d:and關(guān)鍵字運(yùn)用

make.left.right.and.bottom.equalTo(superview);

make.top.equalTo(otherView);

e:優(yōu)先把还;優(yōu)先權(quán)(.priority,.priorityHigh,.priorityMedium,.priorityLow)

.priority允許您指定一個(gè)確切的優(yōu)先級

.priorityHigh 等價(jià)于UILayoutPriorityDefaultHigh

.priorityMedium 介于高跟低之間

.priorityLow 等價(jià)于UILayoutPriorityDefaultLow

實(shí)例:

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();

make.top.equalTo(label.mas_top).with.priority(600);

g:使用mas_makeConstraints創(chuàng)建constraint后,你可以使用局部變量或?qū)傩詠肀4嬉员阆麓我盟蝗绻麆?chuàng)建多個(gè)constraints吊履,你可以采用數(shù)組來保存它們

// 局部或者全局

@property (nonatomic, strong) MASConstraint *topConstraint;

// 創(chuàng)建約束并賦值

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {

self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);

make.left.equalTo(superview.mas_left).with.offset(padding.left);

}];

// 過后可以直接訪問self.topConstraint

[self.topConstraint uninstall];

h:mas_updateConstraints更新約束安皱,有時(shí)你需要更新constraint(例如,動畫和調(diào)試)而不是創(chuàng)建固定constraint艇炎,可以使用mas_updateConstraints方法

- (void)updateConstraints {

[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {

make.center.equalTo(self);

make.width.equalTo(@(self.buttonSize.width)).priorityLow();

make.height.equalTo(@(self.buttonSize.height)).priorityLow();

make.width.lessThanOrEqualTo(self);

make.height.lessThanOrEqualTo(self);

}];

//調(diào)用父updateConstraints

[super updateConstraints];

}

i:mas_remakeConstraints更新約束酌伊,mas_remakeConstraints與mas_updateConstraints比較相似磺芭,都是更新constraint崖瞭。不過润脸,mas_remakeConstraints是刪除之前constraint宴偿,然后再添加新的constraint(適用于移動動畫)吴旋;而mas_updateConstraints只是更新constraint的值涝婉。

- (void)changeButtonPosition {

[self.button mas_remakeConstraints:^(MASConstraintMaker *make) {

make.size.equalTo(self.buttonSize);

if (topLeft) {

make.top.and.left.offset(10);

} else {

make.bottom.and.right.offset(-10);

}

}];

}

十四劲件、UIWebView在IOS9下底部出現(xiàn)黑邊解決方式

UIWebView底部的黑條很難看(在IOS8下不會未巫,在IOS9會出現(xiàn))唇敞,特別是在底部還有透明控件的時(shí)候蔗草,隱藏的做法其實(shí)很簡單,只需要將opaque設(shè)為NO疆柔,背景色設(shè)為clearColor即可

十五咒精、數(shù)組逆序遍歷

1、枚舉法

NSArray *array = @[@"1",@"2",@"3",@"5",@"6"];

[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id? _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%@",obj);

}];

2旷档、for循環(huán)

NSArray*array=@[@"1",@"2",@"3",@"5",@"6"];

for (NSInteger index = array.count-1; index>=0; index--) {

NSLog(@"%@",array[index]);

}

十六模叙、把時(shí)間字符串2015-04-10格式化日期轉(zhuǎn)為NSDate類型

參考答案:

NSString *timeStr = @"2015-04-10";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @"yyyy-MM-dd";

formatter.timeZone = [NSTimeZone defaultTimeZone];

NSDate *date = [formatter dateFromString:timeStr];

// 2015-04-09 16:00:00 +0000

NSLog(@"%@", date);

十七 、簡單寫出增鞋屈、刪范咨、改、查的SQL語句谐区。

參考答案:

數(shù)據(jù)庫的簡單操作湖蜕,還是會的,大學(xué)可沒白學(xué)宋列。

增:

insert into tb_blogs(name, url) values('aaa','http://www.baidu.com');

刪:

delete from tb_blogs where blogid = 1;

改:

update tb_blogs set url = 'www.baidu.com' where blogid = 1;

查:

select name, url from tb_blogs where blogid = 1;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末昭抒,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子炼杖,更是在濱河造成了極大的恐慌灭返,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坤邪,死亡現(xiàn)場離奇詭異熙含,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)艇纺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門怎静,熙熙樓的掌柜王于貴愁眉苦臉地迎上來邮弹,“玉大人,你說我怎么就攤上這事蚓聘‰缦纾” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵夜牡,是天一觀的道長与纽。 經(jīng)常有香客問我,道長塘装,這世上最難降的妖魔是什么急迂? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮蹦肴,結(jié)果婚禮上僚碎,老公的妹妹穿的比我還像新娘。我一直安慰自己冗尤,他們只是感情好听盖,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布胀溺。 她就那樣靜靜地躺著裂七,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仓坞。 梳的紋絲不亂的頭發(fā)上背零,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機(jī)與錄音无埃,去河邊找鬼徙瓶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嫉称,可吹牛的內(nèi)容都是我干的侦镇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼织阅,長吁一口氣:“原來是場噩夢啊……” “哼壳繁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起荔棉,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤闹炉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后润樱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體渣触,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年壹若,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了嗅钻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片皂冰。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖养篓,靈堂內(nèi)的尸體忽然破棺而出灼擂,到底是詐尸還是另有隱情,我是刑警寧澤觉至,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布剔应,位于F島的核電站,受9級特大地震影響语御,放射性物質(zhì)發(fā)生泄漏峻贮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一应闯、第九天 我趴在偏房一處隱蔽的房頂上張望纤控。 院中可真熱鬧,春花似錦碉纺、人聲如沸船万。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽耿导。三九已至,卻和暖如春态贤,著一層夾襖步出監(jiān)牢的瞬間舱呻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工悠汽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留箱吕,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓柿冲,卻偏偏與公主長得像茬高,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子假抄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

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