收納:個(gè)人iOS項(xiàng)目常用效果方法注意點(diǎn)集錦
小白的學(xué)習(xí)路上整理的小知識(shí)點(diǎn):
1、在AUIViewController頁(yè)面上顯示BUIViewController頁(yè)面的半透明背景歹篓。
在A控制器push到B控制器頁(yè)面的方法中:
//設(shè)置半透明背景瘫证,需加載此判斷,否則頁(yè)面效果是使用present方法彈出時(shí)遇到底層view變黑(ios為了保證系統(tǒng)流暢度同一時(shí)間只允許顯示一個(gè)viewController)
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
Bvc.modalPresentationStyle=UIModalPresentationOverCurrentContext;
}else{
self.modalPresentationStyle=UIModalPresentationCurrentContext;
}
隨后在B控制中的init方法中庄撮,設(shè)置背景顏色
//設(shè)置半透明背景
UIColor *color=[UIColor blackColor];
self.view.backgroundColor=[color colorWithAlphaComponent:0.7];
相關(guān)文檔鏈接:
2背捌、導(dǎo)航試圖下的多個(gè)頁(yè)面,設(shè)置直接“返回首頁(yè)”洞斯。
有時(shí)候我們的NavigationController下面會(huì)有好幾層毡庆,進(jìn)到里面的view之后又想返回到上面的幾層view,只要是同一個(gè)NavigationController父類(lèi)下的view都可以用用下面這句代碼烙如。
[self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:([self.navigationController.viewControllerscount] -4)]animated:YES];
這句代碼什么都不用改么抗,只要把數(shù)字“2”改成你要網(wǎng)上跳轉(zhuǎn)的層數(shù)+1就行。
試了一下亚铁,“1”代表當(dāng)前的view蝇刀,“2”代表上一層的view,“3”代表上上層的view徘溢,以此類(lèi)推吞琐。
3、為導(dǎo)航欄主題添加背景和文字顏色:
設(shè)定導(dǎo)航條背景顏色
方法1:[[UINavigationBarappearance]setBarTintColor:[UIColorblackColor]];
方法2:self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
設(shè)定導(dǎo)航欄文字顏色
方法1:[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
方法2:self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
4甸昏、把漢字轉(zhuǎn)換為拼音顽分。(如需用直接調(diào)用方法)
-(NSString*)hanzi2pinyin:(NSString*)str{
NSMutableString * ms = [[NSMutableString alloc]initWithString:str];
if(CFStringTransform(( __bridge CFMutableStringRef)ms,0,kCFStringTransformMandarinLatin,NO)){
NSLog(@"Pingying: %@", ms);// wǒ shì zhōng guó rén
}if(CFStringTransform((__bridge CFMutableStringRef)ms, 0,kCFStringTransformStripDiacritics,NO)) {
NSLog(@"Pingying: %@", ms);// wo shi zhong guo ren
}
return ms;
}
用kCFStringTransformMandarinLatin方法轉(zhuǎn)化出來(lái)的是帶音標(biāo)的拼音,如果需要去掉音標(biāo)施蜜,則繼續(xù)使用kCFStringTransformStripCombiningMarks方法即可。
5雌隅、關(guān)閉或退出鍵盤(pán)的方式:
第一種:點(diǎn)擊return,直接關(guān)閉鍵盤(pán)翻默。(使用條件:能獲取到obj對(duì)象時(shí))(有UITextfileddelegate協(xié)議)
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
return[texresignFirstResponder];
}
第二種:點(diǎn)擊背景View收起鍵盤(pán)(你的View必須是繼承control)(使用條件:有多個(gè)obj對(duì)象時(shí))
[self.view?endEditing:YES];
第三種:你可以在任何地方加上這句話,可以用來(lái)統(tǒng)一收起鍵盤(pán)恰起。
[[[UIapplicationsharedApplication]?keyWindow]?endEditing:YES];
第四種:直接發(fā)送 resignFirstResponder 消息:
[[UIApplication?sharedApplication]?sendAction:@selector(resignFirstResponder)
6 iOS10適配:被棄用的openURL
文檔鏈接:iOS10適配:被棄用的openURL
蘋(píng)果在iOS 2中引入了openURL:方法來(lái)進(jìn)行APP間的跳轉(zhuǎn)修械。不過(guò)在iOS 9中,相關(guān)的canOpenURL:函數(shù)已經(jīng)被私有化了检盼,蘋(píng)果禁止開(kāi)發(fā)者查詢?cè)O(shè)備上是否安裝了某款A(yù)PP肯污。在iOS 10中,蘋(píng)果棄用了openURL怒医,轉(zhuǎn)而用openURL:options:completionHandler:替代豺旬。它可以異步執(zhí)行并在主隊(duì)列中執(zhí)行完成后進(jìn)行回調(diào)(此方法替換原來(lái)的openURL:)。
關(guān)于Options 參數(shù)
UIApplication 頭文件為options字典列出了一個(gè)key:
UIApplicationOpenURLOptionUniversalLinksOnly:如果這個(gè)要打開(kāi)的URL有效贮聂,并且在應(yīng)用中配置它布爾值為true(YES)時(shí)才可以打開(kāi)柬唯,否則打不開(kāi)认臊。
為了覆蓋默認(rèn)行為,創(chuàng)建一個(gè)設(shè)置key值了True的字典作為參數(shù)傳入:
NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly : @YES};
[application openURL:URL options:options completionHandler:nil];
以上面示例锄奢,如果我設(shè)置它為true并打開(kāi)URL:https://twitter.com/kharrison 時(shí), 如果我并沒(méi)有安裝Twitter app那它就會(huì)失敗失晴,同時(shí)會(huì)調(diào)用safari來(lái)打開(kāi)這個(gè)鏈接。