1.iOS11數(shù)字精度問題
/*!
@brief 修正浮點(diǎn)型精度丟失
@param str 傳入接口取到的數(shù)據(jù)
@return 修正精度后的數(shù)據(jù)
*/
+(NSString *)reviseString:(NSString *)str
{
//直接傳入精度丟失有問題的Double類型
double conversionValue = [str doubleValue];
NSString *doubleString = [NSString stringWithFormat:@"%lf", conversionValue];
NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:doubleString];
return [decNumber stringValue];
}
2.iOS 11 tableview適配
// tableView 偏移20/64適配
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也適用
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
3.iOS 11地圖不提示是否允許定位
Privacy - Location Always Usage Description? ?//刪掉這個(gè)iOS11可提示
Privacy - Location When In Use Usage Description
4.高德地圖點(diǎn)擊手勢(shì)添加標(biāo)記 進(jìn)行POI檢索
點(diǎn)擊屏幕上地圖? 獲取手勢(shì)點(diǎn)擊點(diǎn)經(jīng)緯度并進(jìn)行POI檢索的方法
1.添加手勢(shì)
UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
mTap.delegate = self;
[self.mapView addGestureRecognizer:mTap];
2.手勢(shì)對(duì)應(yīng)的方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//這里touchPoint是點(diǎn)擊的某點(diǎn)在地圖控件中的位置
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//這里touchMapCoordinate就是該點(diǎn)的經(jīng)緯度了
AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
request.location? ? ? ? ? ? = [AMapGeoPoint locationWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];
/* 按照距離排序. */
request.sortrule? ? ? ? ? ? = 0;
request.requireExtension? ? = YES;
[self.search AMapPOIAroundSearch:request];
5.多層wkwebview返回
if ([self.localWebView canGoBack]) {
[self.localWebView goBack];
} else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
6.tableview數(shù)組越界
使用懶加載的數(shù)組只創(chuàng)建一次刷新數(shù)據(jù)的時(shí)候要記得移除所有的數(shù)組元素
[self.dataArray removeAllObjects];
判斷數(shù)組為空時(shí)候的越界問題當(dāng)首次數(shù)據(jù)沒有請(qǐng)求完畢的時(shí)候[tableVIew reloadData];就會(huì)導(dǎo)致crash這個(gè)時(shí)候需要做一次判斷:
if(self.dataArray.count != 0){
MOdel * model = self.dataArray[indexPath.row];
}
有時(shí)候會(huì)出現(xiàn)上拉加載更多后點(diǎn)擊下拉出現(xiàn)crash 這個(gè)時(shí)候提示數(shù)組越界但是并不是真的越界 因?yàn)檫@個(gè)時(shí)候的indexpath.row > 數(shù)組的元素個(gè)數(shù)的陷嘴。所以需要以下處理
if(!(indexPath.row > rewardArray.count)){
Model* model = slef.dataArray[indexpath.row];
}
7.textfiled占位語的設(shè)置
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [Toolkit getColor:@"ff5600"];
NSMutableAttributedString *placeHolder = [[NSMutableAttributedString alloc]initWithString:@"? ?請(qǐng)輸入支付密碼" attributes:attrs];
_zhifuTextField.attributedPlaceholder = placeHolder;
8. 禁止屏幕滑動(dòng)返回
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 禁用返回手勢(shì)
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
9.label中劃線設(shè)置
//中劃線
NSMutableAttributedString *attribtStr_origin = [[NSMutableAttributedString alloc]initWithString:originalMoney attributes:attribtDic];
[attribtStr_origin setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle),NSForegroundColorAttributeName:[Toolkit getColor:hex_aaaaaa]} range:NSMakeRange(0,attribtStr_origin.length)];
[attribtStr_origin addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"GillSans" size:12.0] range:NSMakeRange(0,attribtStr_origin.length)];
[attribtStr appendAttributedString:attribtStr_origin];
10.Mansonry不用弱引用 為什么不會(huì)循環(huán)引用
-(NSArray )mas_makeConstraints:(void(^)(MASConstraintMaker ))block {self.translatesAutoresizingMaskIntoConstraints = NO;MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];block(constraintMaker);return [constraintMaker install];}
這個(gè)就和網(wǎng)絡(luò)請(qǐng)求里面使用self道理是一樣的饮戳。因?yàn)閁IView未強(qiáng)持有block,所以這個(gè)block只是個(gè)棧block奕枝,而且構(gòu)不成循環(huán)引用的條件莹汤。棧block有個(gè)特性就是它執(zhí)行完畢之后就出棧夫晌,出棧了就會(huì)被釋放掉〗赫埽看mas_makexxx的方法實(shí)現(xiàn)會(huì)發(fā)現(xiàn)這個(gè)block很快就被調(diào)用了畔塔,完事兒就出棧銷毀,構(gòu)不成循環(huán)引用鸯屿,所以可以直接放心的使用self澈吨。
masonry里面沒有額外引用起來,block執(zhí)行完之后就隨著方法執(zhí)行完之后就銷毀了寄摆,不存在一直被引用釋放不了的問題谅辣,所以無需weak。weak也無所謂婶恼。
11.跨多級(jí)頁面跳轉(zhuǎn)
[[_app_ getTabBar] selectTableBarIndex:2];
[self.navigationController popToRootViewControllerAnimated:NO];
UINavigationController *selectedNavi = [_app_ getTabBar].selectedViewController;
if (selectedNavi.viewControllers.count >0) {
if ([[selectedNavi.viewControllers firstObject] class] == [ProfileViewController class]) {
PropertyDetailViewController *propertyDetailVC =[[PropertyDetailViewController alloc]initWithPropertyType:6];
ProfileViewController *ProfileViewController = [selectedNavi.viewControllers firstObject];
[ProfileViewController.navigationController pushViewController:propertyDetailVC animated:NO];
}
}
12.富文本字體的修改
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"我已閱讀并同意《xxxx商家合作服務(wù)協(xié)議》"];
[str addAttributes:@{NSForegroundColorAttributeName:[Toolkit getColor:@"507daf"]} range:NSMakeRange(7, 13)];
_agreenedLabel.attributedText = str;
13.鍵盤遮擋問題 手勢(shì)沖突
//解決手勢(shì)沖突 網(wǎng)格點(diǎn)擊
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view != self.customCollectionView) {
[self.view endEditing:YES];
return NO;
}
return YES;
}
14.鍵盤輸入框限制輸入等處理
1.先加入事件
[_phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
2.處理方法
- (void)textFieldDidChange:(UITextField *)textField{
UITextRange *selectedRange = textField.markedTextRange;
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position) {
// 沒有高亮選擇的字
// 1. 過濾非漢字桑阶、字母柏副、數(shù)字字符
self.phoneTextField.text = [self filterCharactor:textField.text withRegex:@"[^0-9]"];
// 2. 截取
if (self.phoneTextField.text.length >= 12) {
self.phoneTextField.text = [self.phoneTextField.text substringToIndex:11];
}
} else {
// 有高亮選擇的字 不做任何操作
}
}
// 過濾字符串中的非漢字、字母蚣录、數(shù)字
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
NSString *filterText = string;
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""];
return result;
}
15.oc中的泛型
舉例:
- (void)test {
NSMutableArray *strArray = [NSMutableArray array];
[strArray addObject:@"aString"];
}
可以在聲明NSMutableArray時(shí)添加一個(gè)弱泛型約束割择,之所以是弱泛型,是因?yàn)榫幾g器會(huì)幫你檢查數(shù)據(jù)類型是否正確萎河,如果不正確會(huì)有一個(gè)警告荔泳,但是不會(huì)強(qiáng)制報(bào)錯(cuò),代碼還是可以編譯過的虐杯。
//傳入的不是規(guī)定的類型 會(huì)報(bào)警告
- (void)test {
NSMutableArray *strArray = [NSMutableArray array];
[strArray addObject:[NSNumber numberWithFloat:15.0]];
}
16.修改UIAlertView設(shè)置文字左對(duì)齊
因?yàn)閕phoneSDK默認(rèn)是居中對(duì)齊的玛歌,而且沒有提供方法設(shè)置文本對(duì)齊接口,在Delegate中:
- (void)willPresentAlertView:(UIAlertView *)alertView;
獲取UIAlertView上面的Message控件擎椰,它其實(shí)也是一個(gè)UILable控件支子,然后設(shè)置其textAlignment的屬性即可。
代碼如下:
-(void)willPresentAlertView:(UIAlertView *)alertView{
UIView * view = [alertView.subviews objectAtIndex:2];
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*) view;
label.textAlignment = UITextAlignmentLeft;
}
}
這里要注意的是确憨,Message的UILable在alertView.subviews里面是第3個(gè)元素译荞,第一個(gè)元素是一個(gè)UIImageView(背景),UILable(標(biāo)題),UILable(Message),UIButton(Cancel)...(如果還有的話以此類推)瓤的。
17.iPhone6屏幕獲取不準(zhǔn)的原因
手機(jī)設(shè)置在放大模式 會(huì)導(dǎo)致代碼獲取屏幕尺寸不準(zhǔn) 6 6s 7 寬度和 5s 輸出寬度一直 設(shè)置改為標(biāo)準(zhǔn)模式 消失
CGRect bounds = [[UIScreen mainScreen] bounds];
NSString *screenMode = [[UIScreen mainScreen].coordinateSpace description]; CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat nativeScale = [[UIScreen mainScreen] nativeScale];
NSLog(@" bounds: %@ screen mode: %@ scale: %f native scale: %f", NSStringFromCGRect(bounds), screenMode, scale, nativeScale);
18.本地字典寫成需要的JSON 本地文件
// 1.判斷當(dāng)前對(duì)象是否能夠轉(zhuǎn)換成JSON數(shù)據(jù).
// YES if obj can be converted to JSON data, otherwise NO
BOOL isYes = [NSJSONSerialization isValidJSONObject:dict];
if (isYes) {
NSLog(@"可以轉(zhuǎn)換");
/* JSON data for obj, or nil if an internal error occurs. The resulting data is a encoded in UTF-8.
*/
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
// 將JSON數(shù)據(jù)寫成文件
// 文件添加后綴名: 告訴別人當(dāng)前文件的類型.
// 注意: AFN是通過文件類型來確定數(shù)據(jù)類型的!如果不添加類型,有可能識(shí)別不了! 自己最好添加文件類型.
[jsonData writeToFile:@"/Users/ygkj/Desktop/shopCartTestData.json" atomically:YES];
NSLog(@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
} else {
NSLog(@"JSON數(shù)據(jù)生成失敗休弃,請(qǐng)檢查數(shù)據(jù)格式");
}
19.滾動(dòng)試圖不滾動(dòng)的問題
-(void)viewDidLayoutSubviews
{
_BaseScore.contentSize = CGSizeMake(SCREEN_WIDTH, xxxx);
}