美文网首页
Swift Code Block Backups

Swift Code Block Backups

作者: 申申申申申 | 来源:发表于2016-12-27 10:27 被阅读37次

    简单粗暴 不定期更新(始于161128)......

    1. 打开关闭闪光灯
     let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
     if device.hasTorch {
            do {
                 try device.lockForConfiguration()
            } catch _ {
            }
           device.torchMode = AVCaptureTorchMode.On // .Off
           device.unlockForConfiguration()
     }
    
    1. UISearchBar 去除背景灰色
    let searchBar = UISearchBar.init(frame: CGRect(x: 10, y: 5, width: ScreenWidth-70, height: 30))
    for subview in searchBar.subviews {
         if subview.isKind(of: NSClassFromString("UIView")!) && subview.subviews.count > 0 {
              subview.subviews.first!.removeFromSuperview()
         }
      }
    
    1. 某一页隐藏 navigationbar,下一页显示
     override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(true, animated: true)
     }
    
     override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)
          navigationController?.setNavigationBarHidden(false, animated: true)
     }
    
    1. delegate
     protocol NewHomeTopSearchViewDelegate: NSObjectProtocol {
           func topViewBeginSearch()
     }
    
     class NewHomeTopSearchView: UIView, UISearchBarDelegate {
          var delegate: NewHomeTopSearchViewDelegate?
        
          func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
              if let delegate = delegate {
                  delegate.topViewBeginSearch()
              }
           }
     }
    
    1. SnapKit 是 Masonry 的 Swift 版本,资源地址:MasonrySnapKit
      基本用法如下:
    myPointLabel.snp.makeConstraints { (make) 
           make.top.equalTo(signButton.snp.top).offset(20)
           make.height.equalTo(signButton)
           make.left.equalToSuperview()
           make.right.equalTo(signButton.snp.left).offset(-10)
     }
    
    1. button 整体位置
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
    button.contentVerticalAlignment = UIControlContentVerticalAlignment.top
    
    1. 类方法
    class func newHomeGetItemHeight(ratioWidth: CGFloat, ratioHegiht: CGFloat, width: CGFloat) -> CGFloat {
            return width * ratioHegiht / ratioWidth
        }
    
    1. 碰到了一个约束的问题:依据 屏幕w 动态适应了 itemH,但是由于 item 中 仅仅按照比例设置了 imgView的宽高,又由于 按比例计算了itemH,那么在不同的尺寸下(SE),imgView 与 item 动态的改变了 H,但是其他的控件的高度没变,所以其他控件就不正常的显示了 ---> 解决:如果需要按照比例设置item,那么应该给 item中的 每个控件 都添加比例约束
      xib中 单个控件 按比例设置宽高约束:xib中固定好相对位置(比如 设置好上左右约束后 手动设置正确的高度,然是不要把高度加入约束中,而是图8-0) -> 按比例设置好宽高,但不要添加到约束中 - 图8-0
    图8-0
    图8-1
    图8-2
    1. appdelegate 获取 storyboard 创建的 UITabBarController
    let tabbarVC = self.window?.rootViewController as! UITabBarController
    tabbarVC.tabBar.tintColor = UIColor.init(rgb: 0xFF2832)
    
    1. 修改状态栏的背景色
    extension UIApplication {
          var statusBarView: UIView? {
              return value(forKey: "statusBar") as? UIView
          }
    }  
    UIApplication.shared.statusBarView?.backgroundColor = .red
    
    1. 当 vc 在 nvc 中的时候,需要更改状态栏颜色
    // 状态栏字体为白色,状态栏和导航栏背景为黑色 
    self.navigationController?.navigationBar.barStyle = .black
    // 状态栏字体为黑色,状态栏和导航栏背景为白色
    self.navigationController?.navigationBar.barStyle = .default
    
    1. 获取文字的宽高
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
    
            let text = name_label.text! as NSString
    
            let length = text.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 15), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName : name_label.font], context: nil).size.width + 1
    
            name_label.snp.remakeConstraints { (make) in
                make.top.equalTo(41)
                make.width.equalTo(length)
                make.height.equalTo(15)
                make.left.equalTo(avatarImgView.snp.right).offset(25)
            }
    
        }
    
    1. 获取空间最大y
    signature_label.frame.maxY
    
    1. 初始化一个存放 label 的数组
    private var label_array = Array<UILabel>()
    
    1. 获取当前日期为星期几
            var calendar = NSCalendar.current
    
            calendar.timeZone = TimeZone.init(identifier: "Asia/Shanghai")!
    
            let components = calendar.component(.weekday, from: Date.init())
    
            print(components)
    
    1. 判断点击区域
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            let touch = (touches as NSSet).anyObject() as! UITouch
    
            let point = touch.location(in: self.view)
    
            if !self.collectionView.frame.contains(point) {
    
                self.navigationController?.popViewController(animated: true)
            }
        }
    
    1. 某一个vc 隐藏 statusbar
    override var prefersStatusBarHidden: Bool {
    
            return true
        }
    
    1. 转 str 并保留两位小数
    String.init(format: "%.2f", doublevalue)
    
    1. UIKeyboardWillChangeFrame
     NotificationCenter.default.addObserver(self, selector: #selector(keybordChanged), name: .UIKeyboardWillChangeFrame, object: nil)
    
    1. 图片拉伸
    private lazy var bgImgView: UIImageView = {
            var image = UIImage.init(named: "r_beijing")
            image = image?.stretchableImage(withLeftCapWidth: Int((image?.size.width)!/2.0), topCapHeight: Int((image?.size.height)!/2.0))
    
            let imgView = UIImageView.init()
            imgView.image = image
            imgView.isUserInteractionEnabled = true
            return imgView
        } ()
    

     不定期更新 不合适的地方 还请指点~ 感激不尽

    相关文章

      网友评论

          本文标题:Swift Code Block Backups

          本文链接:https://www.haomeiwen.com/subject/izvopttx.html