姚宁的博客

iOS开发常用Tips

语言相关

  1. swift退出命令行

    ctrl+d

工具相关

  1. pod忽略三方库警告
    :inhibit_warnings => true
    
  2. pod指定Github版本
    :git => '{path}'
    

    eg. eg. pod 'Spring', :git => 'https://github.com/MengTo/Spring.git', :inhibit_warnings => true

  3. swift格式化代码插件
    Swimat
    下载后启动Swimat这个App 根据提示在系统偏好设置里边开启权限
    设置快捷键-> Xcode->Preferences->Key Bindings 搜索swimat 配置上快捷键即可

API相关

  1. navigationItem 使用图片 被tintColor干扰
    let leftItem = UIBarButtonItem.init(image: UIImage.init(named: "nav_logo")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(clickMenu))
    navigationItem.leftBarButtonItem = leftItem
    
  2. 修改TextField光标颜色
    // 全局修改
    UITextField.appearance().tintColor = UIColor.black
    // 单个修改
    let textField = UITextField()
    textField.tintColor = UIColor.black
    
  3. 解析来自server gbk编码的char
    [NSString stringWithCString:cstr encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
    
  4. 实现view跟随手指拖拽
    class EPQuestionCommitButton: UIButton {
    
     override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
         guard let touch = touches.first else {
             return
         }
         let currentPoint = touch.location(in: self.superview)
         let previousPoint = touch.previousLocation(in: self.superview)
    
         var center = self.center
    
         center.x += (currentPoint.x - previousPoint.x)
         center.y += (currentPoint.y - previousPoint.y)
    
         let maxX = (self.superview?.frame.size.width)! - self.frame.size.width / 2
         let minX = self.frame.size.width / 2
         if center.x > maxX {
             center.x = maxX
         } else if center.x < minX {
             center.x = minX
         }
    
         let maxY = (self.superview?.frame.size.height)! - self.frame.size.height / 2
         let minY = self.frame.size.height / 2
         if center.y > maxY {
             center.y = maxY
         } else if center.y < minY {
             center.y = minY
         }
    
         self.center = center
     }
    }
    
  5. 实现WebView加载进度显示
    class EPWebViewController: UIViewController {
     fileprivate lazy var processView: UIProgressView = {
         let processView = UIProgressView()
         processView.tintColor = GlobalColor.happyColor
         return processView
     }()
    
     let pregressKeyPath = "estimatedProgress"
    
     override func viewDidLoad() {
         super.viewDidLoad()
         ...
         webView.addObserver(self, forKeyPath: pregressKeyPath, options: .new, context: nil)
         ...
     }
    
     ...
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
         if keyPath == pregressKeyPath {
             processView.alpha = 1.0
             processView.setProgress(Float(webView.estimatedProgress), animated: true)
             if webView.estimatedProgress >= 1.0{
                 UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseInOut, animations: {
                     self.processView.alpha = 0.0
                 }) { (finished) in
                     self.processView.progress = 0.0
                 }
             }
         }
     }
        
     ...
     deinit {
         webView.removeObserver(self, forKeyPath: pregressKeyPath)
         log.info("EPWebViewController deinit")
     }
    }
    
  6. 日期选择器时间校准
    let timeZone = NSTimeZone.system
    datePicker.date.addTimeInterval(TimeInterval(timeZone.secondsFromGMT(for: datePicker.date)))
    pickerOk!(datePicker.date)
    
  7. 日期选择器初始化阻塞主线程
    UIDatePicker 在viewdidload中
    addsubview加入主界面 耗时大约0.5s  
    建议使用UIActivityIndicatorView先展示  
    将addsubview放在viewDidAppear中做并定死宽高  
    

其他

  1. 处理器架构知识
    在iOS编程时如果要运行在模拟器上,代码生成的机器指令时就需要指定使用i386还是x64指令集,因为目前的mac电脑上基本采用了x86或者x64架构的CPU
    arm64: iPhoneX | iPhone8 | iPhone7 | iPhone6s | iphone6s plus|iPhone6| iPhone6 plus|iPhone5S | iPad Air| iPad mini2(iPad mini with Retina Display)
    armv7s:iPhone5|iPhone5C|iPad4(iPad with Retina Display)
    armv7:iPhone4|iPhone4S|iPad|iPad2|iPad3(The New iPad)|iPad mini|iPod Touch 3G|iPod Touch4
    
    i386是针对intel通用微处理器32位处理器  
    x86_64是针对x86架构的64位处理器
    模拟器32位处理器测试需要i386架构,
    模拟器64位处理器测试需要x86_64架构,
    真机32位处理器需要armv7,或者armv7s架构,
    真机64位处理器需要arm64架构。