iOS-notes

很多来自stackoverflow

代码

  • 使用storyBoard的时候 adding subview to UIImageView 拖一个UIView把它的类型改为UIImageView这时候就能在这个View在添加子视图了。
  • tree 命令一样显示view的层次结构。
[self.view recursiveDescription]
  • tableView滚动的时候始终以row的高度为单位。 首先想到的是tableView继承自scrollView也有pagingEnabled属性。 设置这个属性之后会发现,滚动的时候是以tableViewheight为单位滚的。 实现以下这个代理方法就可以解决了,滚动结束的时候始终停留在某个row上面并且还能够获得indexPath.
-(void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

    // Get index path for target row
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:(*targetContentOffset)];

    // Set new target
    (*targetContentOffset) = [self.tableView rectForRowAtIndexPath:indexPath].origin;
}
  • 去掉tableView多余的行 之前一直根据行数来设置tableView的高度😂其实不用这么麻烦
      self.tableView.tableFooterView = [UIView alloc] init]
    
    • 点击self.view 键盘收起
    - (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [_searchBar endEditing:YES];
    }
    

    其实还有一个神器IQKeyboardManager

    • 更改UI控件系统默认样式
    //黑色改白色 不要忘记设置plist
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //导航条颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    //title的颜色和字体
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
    //透明or不透明。这个会影响storyBoard纵坐标的位置。注意这个方法与`SetbackgroundImageForBarMetrics`一起使用时导航的变化。
    [[UINavigationBar appearance] setTranslucent:NO];
    //serarchBar的颜色,比如说取消按钮。再也不会遍历子视图了。
    [[UISearchBar appearance] setTintColor:[UIColor whiteColor]];
    

    每个控件的appearance都可以去看下。有时候能省很多事。不用一个个的单独设置。

    其他

    • tableView上下拉刷新崩溃 上下刷新频繁操作时数组很容易越界。之前为了解决这个问一直很头大。判断上拉没结束不让下拉,越界的时候刷新tableView之类的。偶尔一次把tableView reload写在了结束刷上下拉刷新方法之前,竟然不崩溃。 >tableView数据源变化之后尽可能早的刷新tableView,这样数组越界的可能性就会降低很多。
    • Xcodearchive之后上传报错 ERROR ITMS-90207: "Invalid Bundle. The bundle at 'example.app' does not contain a bundle executable

    vi打开Info.plist检查有没有特殊字符。有时候从别的地方复制过来的会有特殊字符,删掉重新添加再打包就可以上传了。

    • 在UILabel的text后面增加一些空格,并右对齐. 右对齐后空格无法显示。iOS7+的UILabel会自动将text行尾的空白符全部去除。半角空格\0x20和制表符\t除外。在text后面追加这两个符号即可。
    • Xcode 调试的状态显示indexing....,卡很久
    Open your Project Folder.
    Find ProjectName.xcodeproj file.
    Right-Click Copy and Paste to Safe Place.
    Right-Click Show Package Contents.
    Find project.xcworkspace file and delete that file.
    Reopen Your Project and clean and Rebuild.
    
    • 自定义通知(远程和本地)的声音文件可以放入bundle文件夹,注册推送的时候声音文件填写对应的路径即可。
    let notification = UILocalNotification()
    notification.soundName = "sound.bundle/" + xxx.wav
    
    • Swift printXcode->Devices-> 'xxx的iPhone8' 不会打印日志, NSLog会打印。