需依赖 HandyJSON(用于解析地址 json 数据) 和 IBAnimatable(模态控制器),如果不想导入 IBAnimatable,可自行写模态转场动画 (IBAnimatable 框架的强大,超乎你的想象)
class ViewController: UIViewController {
@IBOutlet weak var addressLabel: UILabel!
/// 懒加载
lazy var addressPicker: WMAddressPicker = {
let nib = UINib(nibName: "WMAddressPicker", bundle: nil)
let picker = nib.instantiate(withOwner: nil, options: nil).first as! WMAddressPicker
picker.modalSize = (width: .full, height: .threeQuarters)
picker.modalPosition = .bottomCenter
picker.cornerRadius = 10
/// 该回调方法可以在本类任意地方写
picker.selectedAreaCompleted = { [weak self] (province, city, district) in
self?.addressLabel.text = province + " " + city + " " + district
}
return picker
}()
/// 调用
@IBAction func selectButonClicked(_ sender: Any) {
self.present(addressPicker, animated: true, completion: nil)
}
}