美文网首页
iOS开发总结

iOS开发总结

作者: AbaryLiu | 来源:发表于2019-04-22 08:40 被阅读0次

1、禁止手机睡眠

[UIApplication sharedApplication].idleTimerDisabled = YES;

2、隐藏某行cell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(indexPath.row == YouWantToHideRow) return 0;
    return 44;
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates];
}

3、禁用button高亮

button.adjustsImageWhenHighlighted = NO;
或者在创建的时候
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

4、tableview遇到这种报错failed to obtain a cell from its dataSource

是因为你的cell被调用的早了。先循环使用了cell,后又创建cell。顺序错了
可能原因:
1、xib的cell没有注册
2、内存中已经有这个cell的缓存了(也就是说通过你的cellId找到的cell并不是你想要的类型),这时候需要改下cell的标识

5、动画切换window的根控制器

// options是动画选项
[UIView transitionWithView:[UIApplication sharedApplication].keyWindow duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        BOOL oldState = [UIView
        areAnimationsEnabled];
        [UIView  setAnimationsEnabled:NO];
        [UIApplication  sharedApplication].keyWindow.rootViewController = [RootViewController new];
        [UIView  setAnimationsEnabled:oldState];
     } completion:^(BOOL finished) {
        
    }];

6、去除数组中重复的对象

NSArray *newArr = [oldArr valueForKeyPath:@“@distinctUnionOfObjects.self"];

7、编译的时候遇到 no such file or directory: /users/apple/XXX

是因为编译的时候,在此路径下找不到这个文件,解决这个问题,首先是是要检查缺少的文件是不是在工程中,如果不在工程中,需要从本地拖进去,如果发现已经存在工程中了,或者拖进去还是报错,这时候需要去build phases中搜索这个文件,这时候很可能会搜出现两个相同的文件,这时候,有一个路径是正确的,删除另外一个即可。如果删除了还是不行,需要把两个都删掉,然后重新往工程里拖进这个文件即可


image

8、iOS8系统中,tableView最好实现下-tableView: heightForRowAtIndexPath:这个代理方法,要不然在iOS8中可能就会出现显示不全或者无法响应事件的问题

9、给一个view截图

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); 
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();

10、开发中如果要动态修改tableView的tableHeaderView或者tableFooterView的高度,需要给tableView重新设置,而不是直接更改高度。正确的做法是重新设置一下tableView.tableFooterView = 更改过高度的view。为什么?其实在iOS8以上直接改高度是没有问题的,在iOS8中出现了contentSize不准确的问题,这是解决办法。

11、注意对象为nil的时候,调用此对象分类的方法不会执行

12、collectionView的内容小于其宽高的时候是不能滚动的,设置可以滚动:

collectionView.alwaysBounceHorizontal = YES;
collectionView.alwaysBounceVertical = YES;

13、由角度转换弧度

#define DegreesToRadian(x) (M_PI * (x) / 180.0)

14、由弧度转换角度

#define RadianToDegrees(radian) (radian*180.0)/(M_PI)

15、获取图片资源

#define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

16、获取window

+(UIWindow*)getWindow {
    UIWindow* win = nil;
    for (id item in [UIApplication sharedApplication].windows) {
        if ([item class] == [UIWindow class]) {
            if (!((UIWindow*)item).hidden) {
                win = item;
                 break;
            }
        }
    }
    return  win;
}

17、修改textField的placeholder的字体颜色、大小

- [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
- [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

18、统一收起键盘

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

19、获取app缓存大小

- (CGFloat)getCachSize {
        NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
        __block NSUInteger count = 0;
        for (NSString *fileName in enumerator) {
            NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
            NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
            count += fileDict.fileSize;
        }
        CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
        return totalSize;
}

20、清理app缓存

- (void)handleClearView {
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDisk];
     NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; 
     [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
}

21、打印百分号和引号

 NSLog(@"%%");
NSLog(@"\"");

22、几个常用权限判断

 if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
        NSLog(@"没有定位权限");
    }
 AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; 
    if (statusVideo == AVAuthorizationStatusDenied) {
       NSLog(@"没有摄像头权限");
    }
AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (statusAudio == AVAuthorizationStatusDenied) { 
        NSLog(@"没有录音权限");
    } 
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    if (status == PHAuthorizationStatusDenied) { 
        NSLog(@"没有相册权限");
    } 
}];

23、长按复制功能

- (void)viewDidLoad {
    [self.view addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pasteBoard:)]];
    } 
- (void)pasteBoard:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
        pasteboard.string = @"需要复制的文本"; 
    } 
}

24、cocoapods升级

在终端执行 sudo gem install -n / usr / local / bin cocoapods --pre
更新cocoapods本地仓库 $ pod repo update
pod库搜索不到SDK,删除索引
rm ~/Library/Caches/CocoaPods/search_index.json,再搜索一次

相关文章

网友评论

      本文标题:iOS开发总结

      本文链接:https://www.haomeiwen.com/subject/gceegqtx.html