iOS 知识小集(0C)

247 阅读3分钟

知识小集目录

1.背景透明,子控件不透明
_bottomView.backgroundColor = [[UIColor akext_colorWithHex:@"#0437F1"] colorWithAlphaComponent:0.5];
 _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
2.iOS11之后 约束刘海
if (@available(iOS 11.0, *)) {
    _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
    // Fallback on earlier versions
    self.automaticallyAdjustsScrollViewInsets = NO;
}

3.UICollectionView 跳转到指定section 常见用于商品详情
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
4.字符串截取
substringFromIndex:从Index开始截取到最后

substringToIndex:从最前头一直截取到Index
5.导入json、geojson文件注意事项

image.png 如果这两不添加是找不到该本地文件的

  NSString *fileName = [[NSBundle mainBundle] pathForResource:@"StoreInfoDat" ofType:@"geojson"];
    //check file exists
    if (fileName) {
      //retrieve file content
      NSData *partyData = [[NSData alloc] initWithContentsOfFile:fileName];

      //convert JSON NSData to a usable NSDictionary
      NSError *error;
      NSDictionary *party = [NSJSONSerialization JSONObjectWithData:partyData
        options:0
          error:&error];
      if (error) {
        NSLog(@"Something went wrong! %@", error.localizedDescription);
      }
      else {
        NSLog(@"party info: %@", party);
      }
    }
   else {
      NSLog(@"Couldn't find file!\n\n");
    }
6.真机模拟器区分
 #if TARGET_IPHONE_SIMULATOR//模拟器
    #elif TARGET_OS_IPHONE//真机
    #endif
7.isExclusiveTouch的使用

待续...

8.UIButton 加载网路图片

导入SDWebImage库文件 在应用的文件中添加#import <SDWebImage/UIButton+WebCache.h>头文件

[btn.imageView sd_setImageWithURL:[NSURL URLWithString:@"按钮url地址"] placeholderImage:[UIImage imageNamed:@"占位图片"]];
9.上拉加载更多不停地请求
   /** 放置tableview 不停的刷新 */
        _baseTableView.estimatedRowHeight = 0;
        _baseTableView.estimatedSectionHeaderHeight = 0;
        _baseTableView.estimatedSectionFooterHeight = 0;
10.tableview上 cell button 点击无响应

最关键的是:按钮要添加到cell的contentview上。否则点击不会生效。

11.系统版本判断
if (@available(iOS 11.0, *)) {
    NSLog(@"iOS 11以上版本");
} else {
    NSLog(@"iOS 11以下版本");
}
12.iOS除法
下面两个代码的运行,你就会发现区别了。

//结果为0.468
CGFloat tFloat = (CGFloat)375/800;
//结果为0
CGFloat tFloat = 375/800;
//结果为0
CGFloat tFloat = 1/0;
//结果为+lnf 代表正无穷
CGFloat tFloat = (CGFloat)1/0;
原因:两个整数相除,结果也为整数
13.数组添加另一个数组或者数组元素
//        //插入的位置
//        NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(1, array.count)];
//        [self.dataArray insertObjects:array atIndexes:indexSet];
[self.dataArray insertObject:array atIndex:1];

一定要可变数组才行

//可变-不可变
NSArray *myArray = [NSArray arrayWithArray: myMutableArray];
//不可变-可变
self.dataArray = [NSMutableArray arrayWithArray:tempArray];
14.表格控件刷新单个section或者row
[self.baseCollectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:2], nil]];
15.数组字符串互转
1.将array数组转换为string字符串
 NSString *str = [array componentsJoinedByString:@"#"];//#为分隔符

输入:[str,kkk,jjj] 输出:str#kkk#jjj

2.将string字符串拆分为array数组[有分割符]
NSArray *array = [str componentsSeparatedByString:@"#"];//#为分隔符

输入: str#kkk#jjj 输出: [str,kkk,jjj]

16.数组的截取
NSArray *array = [NSArray arrayWithObjects:@"Crystal",@"Maisie",@"Lukas",@"Ruben",@"Brooklynn",@"Melody",@"Imogen",@"Skye",@"Rafael",@"Lindsey",@"Felix",nil];
NSArray *topThree = [array subarrayWithRange:NSMakeRange(0, 3)];
NSArray *remaining = [array subarrayWithRange:NSMakeRange(3, array.count-3)];
NSLog(@"topThree:%@=======remaining:%@",topThree,remaining);

17.Masonry 自适应 文字 宽度、高度
[self.storeNameLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
18.更改xcode支持的当前ios版本

image.png

image.png

19.自定义view上添加和移除通知
//这个方法相当于vc中的viewDidLoad
- (void)didMoveToWindow {
    if (self.window) {
        //创建通知
        [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(tongzhi:) name:@"signature" object:nil];
    }
}
//从当前window删除 相当于-viewDidUnload
- (void)willMoveToWindow:(UIWindow *)newWindow {
    if (newWindow == nil) {
//移除通知
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];
    }
}
20.关于NSUIntegerNSInteger

NSUInteger是无符号整型,NSInteger是有符号整型,如果在计算中需要两种数据类型,则需要将无符号NSUInteger转有符号NSInteger,不然会出现各种意想不到的错误.

21.关于权限的相关问题(定位、相机、麦克风等)

ios10+(版本待定),定位:从app 打开手机权限页开启后,可以回到app原页面;相机和麦克风则不会,app会重启。