swift开发常用代码片段

// 绑定事件
cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switchTapped(_:)), for: UIControl.Event.valueChanged)

@objc func switchTapped(_ sender: UISwitch) {
  print(sender.isOn)
}

// 跳转页面
var targetVc: UIViewController
targetVc = targetViewController()
navigationController?.pushViewController(targetVc, animated: true)

// 导入指定cell
if cell == nil {
  cell = Bundle.main.loadNibNamed("cell", owner: nil, options: nil)?.first
}

// 隐藏 tableHeaderView
tableView.tableHeaderView?.removeFromSuperview()
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))

// 隐藏 presentView
self.presentingViewController?.dismiss(animated: true, completion: nil)

// 创建 button
lazy private var logoutButton: UIButton = {
  let logoutButton = UIButton()
  logoutButton.setTitle("退出登录", for: .normal)
  logoutButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  // 文字加粗
  // submitButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
  logoutButton.setTitleColor(UIColor.rs_purple_8127fd, for: .normal)
  logoutButton.addTarget(self, action: #selector(logoutButtonTapped), for: UIControl.Event.touchUpInside)
  return logoutButton
}()

// 在 viewDidLoad() 中
view.addSubview(logoutButton)
  logoutButton.snp.makeConstraints { make in
      make.height.equalTo(40)
      make.width.equalTo(112)
      make.bottom.equalTo(self.bottomLayoutGuide.snp.top).offset(-20)
      make.centerX.equalToSuperview()
}

// 一个弹框
let alertController = UIAlertController(title: "提示", message: "所有人都将看到你的动态,待处理的所有关注请求都将自动获批!", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
                    cell.privacySwitch.setOn(true, animated: true) })
let okAction = UIAlertAction(title: "好的", style: .default, handler: { action in
                    cell.privacySwitch.setOn(false, animated: true) })
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

// 导航栏添加按钮
let submitButton = UIBarButtonItem(title: "提交", style: .plain, target: self, action: #selector(submitButtonTapped))
self.navigationItem.rightBarButtonItem = submitButton

@objc func submitButtonTapped() {
  print("点击了提交...")
}

// 导航栏按钮禁用
self.navigationItem.rightBarButtonItem?.isEnabled = false

// textView 限制字数
lazy private var textView: UITextView = {
  let textView = UITextView(frame: CGRect.zero)
  textView.delegate = self
  textView.text = "输入文字..."
  return textView
}()

extension RSSettingFeedbackViewController: UITextViewDelegate {
  func textViewDidChange(_ textView: UITextView) {
    // 限制字数
    if textView.text.count >= 400 {
        textView.text = String(textView.text.prefix(400))
    }
  }
}

// 拍照或从图库选择图片
// 上传照片
// uploadButton action: uploadButtonTapped
func uploadButtonTapped() {
  let picker = UIImagePickerController()
  picker.delegate = self
  picker.allowsEditing = true

  var sourceType = UIImagePickerController.SourceType.camera
  if !UIImagePickerController.isSourceTypeAvailable(sourceType) {
      sourceType = UIImagePickerController.SourceType.photoLibrary
  }
  picker.sourceType = sourceType
  self.present(picker, animated: true, completion: nil)
}

// 获取当前拍摄或选择的照片
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let photo = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        let image = UIImageView.init(frame: self.view.frame)
        image.image = photo
        uploadButton.setImage(photo, for: UIControl.State.normal)
    }
    picker.dismiss(animated: true, completion: nil)
}

// cancel后执行的方法
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  picker.dismiss(animated: true, completion: nil)
}

// 打开连接(页面跳转)
let str = NSString(format: "itms-apps://itunes.apple.com/app/id%@?action=write-review", VendorConfig.tagAPPID)
UIApplication.shared.openURL(NSURL(string: str as String)! as URL)

// 一个搜索框
lazy var textField: UITextField = {
  let textField = UITextField(frame: CGRect.zero)
  let leftView = UIImageView(image: UIImage(named: "release_search")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
  textField.leftView = leftView
  textField.leftViewMode = .always
  leftView.contentMode = .center
  leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 20)
  textField.clearButtonMode = .whileEditing
  textField.placeholder = "搜索..."
  textField.backgroundColor = UIColor.rs_gray_f2f2f2
  textField.font = UIFont.rs_pingfangsc_regular(size: 14)
  textField.cornerRadius = 8
  textField.delegate = self
  return textField
}()

// 添加分隔线
let topLine = UIView()
topLine.backgroundColor = UIColor.rs_gray_d8d8d8

topLine.snp.makeConstraints { (make) in
    make.left.equalTo(15)
    make.right.equalTo(-15)
    make.top.equalTo(addressCell.snp.top).offset(-1)
    make.height.equalTo(1 / kScale)
}

猜你喜欢

转载自www.cnblogs.com/wx1993/p/10276036.html