美文网首页iOS
iOS那些好用的tips

iOS那些好用的tips

作者: iOSPeter | 来源:发表于2017-02-06 14:51 被阅读49次

    后续会逐步添加...

    1. 苹果提供的UIProgressView高度固定为2,有时候我们就想它变高些,比如想它高度变为5,改变frame或者设置约束发现无效,可以使用如下方式:

    CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 2.5f);
    progressView.transform = transform;
    

    2. 子类视图超出父类视图的部分不想要,有两种办法:

    1. 设置子视图view的clipsToBounds属性为YES。
    2. 设置子视图view.layer的masksToBounds属性为YES。
    

    3. iOS 上传图片限制大小可以使用分类UIImage+Resize

    - (NSData *)resizeImageToTargetSize:(CGSize)targetSize maxDataSize:(NSInteger)maxDataSize {
    // 设置缺省标识尺寸
    if (CGSizeEqualToSize(targetSize, CGSizeZero)) {
        targetSize = CGSizeMake(1024, 1024);
    }
    // 判断尺寸,进行尺寸处理
    CGSize newSize = CGSizeMake(self.size.width, self.size.height);
    CGFloat tempHeight = newSize.height / targetSize.height;
    CGFloat tempWidth = newSize.width / targetSize.width;
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(self.size.width / tempWidth, self.size.height / tempWidth);
    }
    else if (tempHeight > 1.0 && tempWidth < tempHeight){
        newSize = CGSizeMake(self.size.width / tempHeight, self.size.height / tempHeight);
    }
    
    // 确认要处理的图片
    UIImage *newImage = nil;
    if (tempWidth > 1.0 || tempHeight > 1.0) { // 满足压缩条件
        UIGraphicsBeginImageContext(newSize);
        [self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    } else { // 不需要压缩(在正常范围内,保证清晰)
        newImage = self;
    }
    
    // 获取图片大小
    NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
    CGFloat sizeOriginKB = imageData.length / 1024.0;
    
    // 图片大小处理
    CGFloat resizeRate = 0.9;
    while (sizeOriginKB > maxDataSize && resizeRate > 0.1) {
        imageData = UIImageJPEGRepresentation(newImage,resizeRate);
        sizeOriginKB = imageData.length / 1024.0;
        resizeRate -= 0.1;
    }
    
    return imageData;
    }
    

    4. 改变UITextField的placeholder的字体和颜色

     [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]
     [textField setValue:[UIFont systemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"]
    
     如果以上设置方法Xcode发生崩溃,可以使用如下方法:
     // 创建placeholder富文本属性
     NSMutableAttributedString *placeholderMAttributesString = [[NSMutableAttributedString alloc] initWithString:@"请输入您的姓名"];
    // 设置placeholder字体大小
    [placeholderMAttributesString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, placeholderMAttributesString.length)];
    // 设置placeholder颜色
    [placeholderMAttributesString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, placeholderMAttributesString.length)];
    // 设置placeholder
    textField.attributedPlaceholder = placeholderMAttributesString;
    

    5. 【iOS8及以下】与【iOS9及以上】系统实现系统UITableViewCell侧滑坑点

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    }
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
    }
    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath  *)indexPath {
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"设置"  handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
    }];
    action.backgroundColor = [UIColor blueColor];
    
    return @[action];
    }
    
    以上代码即可实现iOS9及以上系统UITableViewCell侧滑,但是运行在iOS8上会发现侧滑不可用。
    解决办法:
    // 此方法不能删,否则iOS8侧滑没反应
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath
    {
    
    }
    

    6. 获取启动图片

    + (UIImage *)launchImage {
    UIImage *image = nil;
    NSArray *launchImages = [NSBundle mainBundle].infoDictionary[@"UILaunchImages"];
    
    for (NSDictionary *dict in launchImages) {
        // 1. 将字符串转换成尺寸
        CGSize size = CGSizeFromString(dict[@"UILaunchImageSize"]);
        
        // 2. 与当前屏幕进行比较
        if (CGSizeEqualToSize(size, [UIScreen mainScreen].bounds.size)) {
            NSString *filename = dict[@"UILaunchImageName"];
            image = [UIImage imageNamed:filename];
            
            break;
        }
    }
    return image;
    }
    

    7. 控制状态栏颜色

    状态栏变白:
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    状态栏变黑:
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    

    8. 获取自己的App在苹果商店最新的版本

    https://itunes.apple.com/lookup?id=xxx
    xxx 改为苹果为自己的App分配的applied
    获取如下:
     NSArray *array = responseObject[@"results"];
     NSDictionary *dict = [array lastObject];
     NSLog(@"当前版本为:%@", dict[@"version"]);
    

    9. 设置某些文件以非ARC编译

     -fno-objc-arc
    

    10. 查看.a静态库支持的CPU架构

     lipo -info xxx.a
    

    11. 强制清除Xcode警告

      #pragma clang diagnostic push  
      #pragma clang diagnostic ignored "xxx"  
      // 这里放有xxx警告的代码 
      #pragma clang diagnostic pop  
    
      注:xxx是一般在警告详情里有,通过[]包裹,声明未使用变量就会出现[-Wunused-variable] 中括号内的内容即为xxx的值
    

    12. 设置导航按钮左右移动

      // 导航右按钮
      UIBarButtonItem *searchButtonItem = [UIBarButtonItem createBarButtonItemWithTitle:@"搜索" titleColor:nil fontSize:0 target:self action:@selector(search)];
      // 位移按钮
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
      //  rightBarButtonItem的场合width为负数时,表示检索btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整为0;width为正数 时,正好相反,表示往左移动width数值个像素
     // 至于width的正负不清楚的,可以自行调试
    negativeSpacer.width = -3;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, searchButtonItem];
    

    13. 控制器继承自UITableViewController,默认创建plain风格,想改为grouped,可以如下操作:

    - (instancetype)initWithStyle:(UITableViewStyle)style {
      return [super initWithStyle:UITableViewStyleGrouped];
    }
    

    14. 使用系统方法使用图片创建UIBarButtomItem,背景色显示蓝色

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    
    解决办法如下:
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_index_nav_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    

    15. 去除UITextView四个内边距

    self.briefTextView.textContainer.lineFragmentPadding = 0; 
    self.briefTextView.textContainerInset = UIEdgeInsetsZero;
    

    16. 隐式动画的控件响应不了点击事件

    UIView做动画的时候把options设置UIViewAnimationOptionAllowUserInteraction
    

    下面这些文章,个人感觉比较实用!有兴趣的可以看看~
    多年iOS开发经验总结(一)
    http://www.jianshu.com/p/1ff9e44ccc78
    多年iOS开发经验总结(二)
    http://www.jianshu.com/p/9fcd37c0ea05

    相关文章

      网友评论

        本文标题:iOS那些好用的tips

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