iOS Tips(无限期更新...)

  • ios - 设置tabBarItem.imageInsets后,item每点一下会变小解决方案
 UIEdgeInsetsMake 的top bottom left right 必须是对称的比如5,-5
exp:
vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);  (显示正常)
vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -4, 0); (每次点击 item 会变小)
  • Masonry label 显示多行 设置 preferredMaxLayoutWidth
 //显示多行,自适应高度
    UILabel *label3 = [[UILabelalloc] initWithFrame:CGRectZero];
    [self.viewaddSubview:label3];
    label3.backgroundColor =[UIColorredColor];
    label3.text =@"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试!";

/* // Support for constraint-based layout (auto layout)
// If nonzero, this is used when determining -intrinsicContentSize for multiline labels

@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0); 

*/
    label3.preferredMaxLayoutWidth = (self.view.frame.size.width -10.0 * 2);
    label3.numberOfLines =0;
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.0);
        make.right.mas_equalTo(-10.0);
        make.top.mas_equalTo(100.0);
    }];
  • 获取App 相关信息接口
http://itunes.apple.com/cn/lookup?id=#appid#  
注意 appid 替换成 自己App的id
  • iOS webView 滑动不流畅 (改变webView 阻尼系数)
/** 改变webView 阻尼系数 */
 _webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
  • iOS 打包的那些坑
Deployment 为 iOS 7.0 打包出来的ipa中 Assets.car 文件 大约比 Deployment 为 iOS 8.0 打包出来的ipa中 Assets.car 小一半左右  Deployment 为 iOS 9.0 打包出来的Assets.car 文件和 7.0 下差不多 
  • view快速添加模糊效果
 UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"index"];
    [self.view addSubview:imageView];
    
     /** 在希望模糊的view上加一个 toolBar 即可 */
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:imageView.frame];
    [imageView addSubview:toolBar];
    
  • 对数组进行排序
#pragma mark 数组排序4-高级排序  
void arraySort4() {  
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];  
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];  
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];  
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];  
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil];  
      
    // 1.先按照书名进行排序  
    // 这里的key写的是@property的名称  
    NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];  
    // 2.再按照姓进行排序  
    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];  
    // 3.再按照名进行排序  
    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];  
    // 按顺序添加排序描述器  
    NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil nil];  
      
    NSArray *array2 = [array sortedArrayUsingDescriptors:descs];  
      
    NSLog(@"array2:%@", array2);  
}  
  • iOS 7.0之后 UILabel展示 HTML文本方法

    NSString * html = @"<html><body><style>body{background-color:#FFFFFF;color:#555555;}img {max-width:200px;max-height:200px;}</style> <p><img src=http://tp.mnks.cn/tp_105.jpg></p>Some<input type='text' size='5'> <br/>html <strong>string</strong> <b>hdhs</b><i>d</i> </body></html>";
    
    NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    lable.attributedText = attr;
  • swift单例写法
class ControversyManager {
    static let sharedInstance = ControversyManager()
}
  • 利用UIApplication单例拿到当前视图控制器 这样就能直接拿到当前的rootViewController。比如对它调用presentViewController: animated: completion:
    就可以马上在当前状态下弹出一个视图控制器。
[[UIApplication sharedApplication].windows firstObject].rootViewController
  • tableView cell 滑动时候实现 3D效果
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    ///配置 CATransform3D 动画内容
    CATransform3D  transform ;
    transform.m34 = 1.0/-800;
    //定义 Cell的初始化状态
    cell.layer.transform = transform;
    //定义Cell 最终状态 并且提交动画
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:1];
    cell.layer.transform = CATransform3DIdentity;
    cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    [UIView commitAnimations];
    
}
  • Label 简单图文混排
// 文字图片拼接显示
- (void)setLabel3
{
    // NSTextAttachment - 附件
    // 1.创建文本附件包含图片,知道附件 bounds
    NSTextAttachment *attachMent = [[NSTextAttachment alloc] init];
    
    // 设置图片
    attachMent.image = [UIImage imageNamed: @"image"];
    
    // 设置大小
    CGFloat height = self.label.font.lineHeight;
    attachMent.bounds = CGRectMake(0, 0, height, height);
    
    // 添加
    // 2.使用附件创建属性字符串
    NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachMent];
    
    // 拼接文字
    NSString *str = @"测试";
    // 3.创建可变字符 拼接字符串
    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithString:str];
    [strM appendAttributedString:attrString];
    [strM appendAttributedString: [[NSAttributedString alloc] initWithString: @"测试"]];
    
    // 设置 label 内容
    self.label.backgroundColor = [UIColor grayColor];
    self.label.attributedText = strM;
}
  • 强制横屏 ios8 横屏状态栏不显示解决方法
:在plist文件中将 View controller-based status bar appearance 设置为NO 在application:didFinishLaunchingWithOptions:中添加下面代码 
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
 [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  • UIMenuController注意事项
显示UIMenuController前必须调用becomeFirstResponder 例如  [self.view becomeFirstResponder];
必须重写canBecomeFirstResponder方法返回YES
有些控件会有系统的UIMenuItem,使用canPerformAction:withSender:方法筛选出需要的item

Tip


学习的路上总是曲折的,每个人都是从菜鸟过来的,遇到问题总是希望能够与他人沟通交流,而在各种群里问了问题就石沉大海,所以想建一个技术交流为主的群,遇到的问题可以记录下来分享给他人,方便了自己,也造就了他人,不管怎样,记录点滴,但愿与君共勉

*QQ群号:527377492 *

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

推荐阅读更多精彩内容