整理IOS开发常用小Tips

在NavigationController中 重写 show/pushViewController方法统一在导航栏中添加按钮

- (void)showViewController:(UIViewController *)vc sender:(id)sender
{
    if (self.viewControllers.count > 0) { //首页不显示跳转可以通过只添加leftBtn来间接修改所有返回按钮的图片
       
    vc.
hidesBottomBarWhenPushed = YES;
       
    vc.
navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
   
    vc.
navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
       
    }
    [
super showViewController:vc sender:sender];
   
}

- (void)back
{
    [
self popViewControllerAnimated:YES];
}

计算文件/文件夹体积  并删除

 NSFileManager *mgr = [NSFileManager defaultManager];
   
   
NSString *rootPath = @"/Users/wanghong/Desktop/test";
   
   
NSArray *subPaths = [mgr subpathsAtPath:rootPath];
   
   
long long totolSize = 0;

   
for (NSString *pathString in subPaths) {
       
NSString *path = [rootPath stringByAppendingPathComponent:pathString];
       
//NSLog(@"%@",path);
       
       
BOOL dir = NO;
       
        [mgr
fileExistsAtPath:path isDirectory:&dir];
       
if (dir == NO) {
           
            totolSize += [[mgr
attributesOfItemAtPath:path error:nil][NSFileSize] longLongValue];
        }
    }
   
   
CGFloat size = totolSize/1000.0/1000.0;  //size结果字节为单位
   
_sizeLabel.text = [NSString stringWithFormat:@"%fMB",size];
   
   
//CLEAN 
    [mgr
removeItemAtPath:rootPath error:nil];


修改UIView或其子类的边框为圆角
[btn.layer setCornerRadius:(btn.frame.size.height/2)];
        btn.layer.borderWidth = 2.0f;
    self.diskBtn.layer.masksToBounds = YES;(有时候需要裁剪)


SDWebImage 清除位于ache文件夹下的缓存 
[[SDWebImageManager sharedManager].imageCache clearDisk]; 用clear不是clean


将image存储到相机胶卷中
UIImage *image = [UIImage imageNamed:@"ceshi"];
   
UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);

创建屏幕快照,返回UIView(用CoreGraphic可以转为UIImage)
[self.view snapshotViewAfterScreenUpdates:NO]



UIPanGestureRecognizer 有时候会影响 UITableView 的 Scroll 操作。由于你已经设置了 Cell 的 Pan 手势识别器 的 UIGestureRecognizerDelegate ,你只需要实现一个(有些滑稽且冗长命名的) delegate 方法即可将一切恢复正常。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
   
return NO;
   
}

利用UIWebView播放GIF 
  NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:self.imageName ofType:@"gif"]];
   
self.gifView = [[UIWebView alloc]init];
   
self.gifView.frame = CGRectMake(0, 0, self.frame.size.width, kCellHeight);
   
   
self.gifView.userInteractionEnabled = NO;
    [
self.gifView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    self.gifView.scalesPageToFit = YES;


重写push控制器的方法取消导航控制器的动画
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   
// 不需要动画
    [
super pushViewController:viewController animated:NO];
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。