// // ImagePickerPopup.swift // HealthyZG // // Created by 林 on 2020/6/17. // Copyright © 2020 Lin. All rights reserved. // import Foundation import RxSwift import SnapKit import RxCocoa class ImagePickerPopup: PopupViewController { var disposeBag = DisposeBag() var cameraAction: (() -> Void)? var albumAction: (() -> Void)? init() { super.init(style: .sheet) self.addCustomView { contentView in contentView.backgroundColor = UIColor.white var cameraButton: UIButton? if UIImagePickerController.isSourceTypeAvailable(.camera) { cameraButton = UIButton(type: .system) cameraButton?.setTitle(NSLocalizedString("拍照", comment: ""), for: .normal) cameraButton?.titleLabel?.font = .systemFont(ofSize: 16) cameraButton?.setTitleColor(UIColor(hexStr: "#454545"), for: .normal) cameraButton?.backgroundColor = UIColor.white } if let cameraButton = cameraButton { contentView.addSubview(cameraButton) // cameraButton.addBorder(edge: .bottom, width: 0.5, color: UIColor.themeLC2) } let albumButton = UIButton(type: .system) albumButton.setTitle(NSLocalizedString("相册", comment: ""), for: .normal) albumButton.titleLabel?.font = .systemFont(ofSize: 16) albumButton.setTitleColor(UIColor(hexStr: "#454545"), for: .normal) albumButton.backgroundColor = UIColor.white contentView.addSubview(albumButton) let cancelButton = UIButton(type: .system) cancelButton.setTitle("取消".localizedString, for: .normal) cancelButton.titleLabel?.font = .systemFont(ofSize: 16) cancelButton.setTitleColor(UIColor(hexStr: "#454545"), for: .normal) cancelButton.backgroundColor = UIColor.white contentView.addSubview(cancelButton) let separatorView = UIView(frame: .zero) separatorView.backgroundColor = UIColor(hexStr: "#EEEEEE") contentView.addSubview(separatorView) if let cameraButton = cameraButton { cameraButton.snp.makeConstraints { make in make.top.left.right.equalToSuperview() make.height.equalTo(60.0) } albumButton.snp.makeConstraints { make in make.top.equalTo(cameraButton.snp.bottom) make.left.right.equalToSuperview() make.height.equalTo(60.0) } } else { albumButton.snp.makeConstraints { make in make.top.left.right.equalToSuperview() make.height.equalTo(60.0) } } separatorView.snp.makeConstraints { make in make.top.equalTo(albumButton.snp.bottom) make.left.right.equalToSuperview() make.height.equalTo(10.0) } cancelButton.snp.makeConstraints { make in make.top.equalTo(separatorView.snp.bottom) make.left.right.equalToSuperview() if #available(iOS 11.0, *) { let bottom = UIApplication.keyWindow?.safeAreaInsets.bottom ?? 0 make.bottom.equalTo(contentView.safeAreaLayoutGuide.snp.bottom).offset(bottom * 0.6) } else { make.bottom.equalTo(contentView.snp.bottom) } make.height.equalTo(60.0) } cameraButton?.rx.tap.subscribe(onNext: { [weak self] _ in guard let self = self else { return } self.hide(callback: { self.cameraAction?() }) }).disposed(by: disposeBag) albumButton.rx.tap.subscribe(onNext: { _ in self.hide(callback: { self.albumAction?() }) }).disposed(by: disposeBag) cancelButton.rx.tap.subscribe(onNext: { [weak self] _ in self?.hide() }).disposed(by: disposeBag) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }