iOS-杂谈(零碎)

零碎问题收集,学习永无止境。这些问题很小,开发却可能经常遇到,需要的时候来查,或者零碎的时间看看也不错,只要还在开发iOS就会一直更新。

1,图片拉伸(如聊天背景)

直接选中图片,slicing 属性:选择水平和垂直保护(left=width/2,ritht = width/2-1,top = height/2,bottom = height/2-1)

2,状态栏设置

//状态栏颜色
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
//状态栏隐藏
-(BOOL)prefersStatusBarHidden{
return NO;
}

3,使用cocoaPods import导入时没有提示的解决办法

1. 选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths
2.双击空白区域,点击“+”号添加一项:并且输入:$(PODS_ROOT),选择:recursive(会在相应的目录递归搜索文件)

4,xcode自定义模板位置

~/Library/Developer/Xcode/UserData/CodeSnippets/

5,总结控制器之间的传值

顺传:上一级控制器传递给下一级控制器,直接能拿到下一级控制器,拿到之后,做什么事情都行。     
逆传:下一级控制器传递给上一级控制器,用代理。上一级控制器作为下一级的代理,监听下一级控制器的事情。
block回调:用于逆传,类似代理

6,navigationBar

navigationBar高度44,UITabBar的高度为49.

7,主流框架的搭建

1.窗口的根控制器为一个UITabBarController
2.把导航控制器添加给UITabBarController,成为UITabBarController的子控制器.
这样做的好处:每一个导航条的内容都由它栈顶控制器来决定.
 push时隐藏底部tabBar.
 self.hidesBottomBarWhenPushed = YES;

8,modal(模态框)

当需要弹出没有必然联系但是需要处理的控制器时
[self presentViewController:twoVC animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

9,block回调

传值方:类的.h有block属性,类的.m有block调用(传值),相当于调用代理方法
接收方(回调):调用该类的block属性,写自己的代码,就和代理在此时重写方法一样

10,加载时hud一闪而过解决方式

//延迟加载
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.view showError:@"没有网络连接,请检查网络后重试"];
        });

11,判断系统版本

 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//处理
}

12,代码加载storyboard中的ViewController

UIStoryboard *board = [UIStoryboard storyboardWithName: @"Main" bundle: nil];
        
ViewController1 *VC  = [[ViewController1 alloc]init];
        
VC = [board instantiateViewControllerWithIdentifier: @"ViewController1"];
        
[[UIApplication sharedApplication].keyWindow setRootViewController:VC];
//此处可以选择设置为窗口的根控制器或者选择push或者modal的方式加载VC

13,加载tableView的 cell 高度动态布局

有返回估计高度的代理方法执行顺序estimatedHeightForRowAtIndexPath->cellForRowAtIndexPath->heightForRowAtIndexPath
无返回估计高度的代理方法执行顺序heightForRowAtIndexPath->cellForRowAtIndexPath
而 collectionView虽然可以返回预估 cell 大小,但是却仍然是第二种执行方式,即先布局再加载数据,如果布局为0,则不会执行加载数据的方法,所以必须先布局 cell 才能加载 cell

设置模型数据使传入 model

- (void)setModel:(Model *)model
{
    _model = model;
重新布局后计算高度
}

14,设置lable文字颜色不同

-(void)setLabelColor:(UILabel *)uiLabel : (NSMutableString *)searchallStr : (NSString *)searchContent{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:searchallStr];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:searchContent].location, [[noteStr string] rangeOfString:searchContent].length);
    //需要设置的位置
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
    //设置颜色
    [uiLabel setAttributedText:noteStr];
    
}

15,两个界面有直接的联系,用代理或者 block,没有直接的联系,用通知

16,屏幕适配,尽量使用屏幕宽度和高度,这样计算出来,一般都是正确的

17,xib显示或者隐藏控件约束变化

给约束会变化的控件设置两个不同优先级的约束,显示或者隐藏时会自动适配优先级.

取多个随机的数组元素

NSArray *array = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",nil];
NSMutableSet *randomSet = [[NSMutableSet alloc] init];

while ([randomSet count] < 3) {
    int r = arc4random() % [array count];
    [randomSet addObject:[array objectAtIndex:r]];
}
    
NSArray *randomArray = [randomSet allObjects];
NSLog(@"%@",randomArray);

你可能感兴趣的:(iOS-杂谈(零碎))