iOS那些好用的tips

后续会逐步添加...

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容