iOS开发一些小技巧

2018/10/15 posted in  iOS
  • 去掉xcode黄色警告的方法

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    your code with warning
    #pragma clang diagnostic pop
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wunused-variable"
    your code with warning
    #pragma clang diagnostic pop
  • 控制器init方法执行时间

    _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    在控制器init方法中初始化一个大小为self.view.bounds的uiview必须等待视图出来的时候才会进行,要不然一直在主线程等待。
    如果直接给CGRectmake设置的话不用等界面加载就会执行,应该是一个主线程问题。
    这个时候如果直接去xib里面的控件,就会出现取不到的情况,取出来是nil。
  • 判断字符串为空

    今天遇到个问题,后台用java写的,给我传回一个空字符串,类型是NSNull,用
    stringWithFormat:转出来居然是@"<null>",
    注意,这不是空,NSNull和nil是不同的。需要用 xxx == [NSNull null]加以判断。
    去掉UITableView多余的空白行分割线
    有一种简单的方法
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  • 如何将本地代码提交到远程git仓库

    * 第一步:建立git仓库 
    cd到你的本地项目根目录下,执行git命令 
    git init
    * 第二步:将项目的所有文件添加到仓库中
    git add .
    * 第三步:将add的文件commit到仓库
    git commit -m "注释语句"
    * 第四步:去github上创建自己的Repository
    * 第五步:重点来了,将本地的仓库关联到github上
    git remote add origin 仓库url
    后面的https链接地址换成你自己的仓库url地址,也就是上面红框中标出来的地址
    * 第六步:上传github之前,要先pull一下,执行如下命令:
    git pull origin master
    * 第七步,也就是最后一步,上传代码到github远程仓库
    git push -u origin master
    git push -u origin master -f //强制push
    每次如果有代码需要提交必须要add一下 并且在本地commit 打下log 再去拉取remote代码,
    然后push到remote的这样一个流程。
  • button 的文字显示不全

     [label setAdjustsFontForContentSizeCategory:YES]调节字体大小.
    
  • 从xib加载View

    // Instantiate the nib content without any reference to it.
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"EPPZPlainView" owner:nil options:nil];
    // Find the view among nib contents (not too hard assuming there is only one view in it).
    UIView *plainView = [nibContents lastObject];
    // Some hardcoded layout.
    CGSize padding = (CGSize){ 22.0, 22.0 };
    plainView.frame = (CGRect){padding.width, padding.height, plainView.frame.size};
    // Add to the view hierarchy (thus retain).
    [self.view addSubview:plainView];
  • 修改导航条的颜色

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];```
    
  • 当某个UIView调用drawRect方法之后,它的默认背景就是黑色的

    解决办法有在初始化的时候设置backgroundColor

    - (void)awakeFromNib {
        [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    // Initialization code
    }
    - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    self.backgroundColor = [UIColor blueColor]
    }
    return self;
    }
  • 或者在drawRect里面写下如下代码

    - (void)drawRect:(CGRect)rect
    {
    // Drawing code
    [[UIColor blueColor] setFill]; // changes are here
    UIRectFill(rect); // and here
    }
  • 主线程死锁

        NSLog(@"1---%@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_main_queue(), ^{
    NSLog(@"2");
    });
    NSLog(@"3");
    NSLog(@"1---%@",[NSThread currentThread]);
    dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"2");
    });
    NSLog(@"3");
    //打印顺序 1,3,2
    先加入的通知先收到通知
    先去执行通知的内容
  • 解决tableviewsection悬浮的问题

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGFloat sectionHeaderHeight = 50;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
    scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
    scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
    }
  • NSURLSession请求

     NSURLSession * session = [NSURLSession sharedSession];
        NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@",dic);
    }];
    [task resume];
  • 实现点击按钮出发一次震动,比如“即刻”app的点赞

    导入AudioToolBox.framework
    在按钮的点击方法中执行
    AudioServicesPlaySystemSound(kSystemSo undID_Vibrate);//上面是震动方法 不灵巧
    可以使用最新的
    UIImpactFeedbackGenerator * zhend = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleLight];
    [zhend impactOccurred];
  • getter方法中为何不能用self

    有经验的开发者应该都知道这一点,在getter方法中是不能使用self.的,比如:

    - (NSString *)name
    {
    NSLog(@"rewrite getter");
    return self.name; // 错误的写法,会造成死循环
    }

    原因代码注释中已经写了,这样会造成死循环。这里需要注意的是:self.name实际上就是执行了属性name的getter方法,getter方法中又调用了self.name, 会一直递归调用,直到程序崩溃。