【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)

时间:2022-09-17 11:11:33
typealias PopPickerViewCallBackClosure = (_ resultStr:NSString?) -> ()
class PopPickerView : UIView
{
var dismissCallBack = {}
var rowAndComponentCallBack:PopPickerViewCallBackClosure? fileprivate var blockContent : NSString? var titleLabel : UILabel?
var divideLine : UIView?
var confirmButton : UIButton?
var cancelButton : UIButton?
var overlayView : UIControl?
var keyWindow : UIWindow? override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.white; if (keyWindow == nil) {
self.keyWindow = UIApplication.shared.keyWindow
} overlayView = UIControl.init(frame: UIScreen.main.bounds)
overlayView?.backgroundColor = UIColor.init(red: , green: , blue: , alpha: 0.5)
overlayView?.addTarget(self, action: #selector(hide), for: .touchUpInside)
overlayView?.alpha = let toolView:UIView = UIView.init(frame: CGRect.init(x: , y: , width: Int(self.bounds.size.width), height: Int()))
toolView.backgroundColor = .white
addSubview(toolView) cancelButton = UIButton.init(frame: CGRect.init(x: , y: , width: , height: toolView.bounds.size.height))
cancelButton?.setTitle("取消", for: .normal)
cancelButton?.setTitleColor(newColor(, , ), for: .normal)
cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: )
cancelButton?.contentHorizontalAlignment = .left
cancelButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
toolView.addSubview(cancelButton!) confirmButton = UIButton.init(frame: CGRect.init(x: (toolView.bounds.size.width - - ), y: , width: , height: toolView.bounds.size.height))
confirmButton?.setTitle("确定", for: .normal)
confirmButton?.setTitleColor(newColor(, , ), for: .normal)
confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: )
confirmButton?.contentHorizontalAlignment = .left
confirmButton?.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
toolView.addSubview(confirmButton!) titleLabel = UILabel.init(frame: CGRect(x: CGFloat(Int(self.bounds.size.width)/ - ), y: , width: , height: toolView.bounds.size.height))
titleLabel?.text = "汽车排量"
titleLabel?.textColor = newColor(, , )
titleLabel?.font = UIFont.systemFont(ofSize: )
titleLabel?.textAlignment = .center
toolView.addSubview(titleLabel!) divideLine = UIView.init(frame: CGRect(x: , y: (confirmButton?.superview?.frame.maxY)!, width: toolView.bounds.size.width, height: ))
divideLine?.backgroundColor = colorWithHexString("0xe5e5e5")
toolView.addSubview(divideLine!)
} convenience init(frame: CGRect,dataSource:NSArray,title:String) {
self.init(frame: frame)
titleLabel?.text = title
if (dataSource.count != ) { let picker = PickerViewBuilder.init(frame: CGRect.init(x: , y: ((confirmButton?.superview?.frame.maxY)! + ), width: UIScreen.main.bounds.size.width - CGFloat(), height: CGFloat()),dataSource:dataSource,contentCallBack:{ [weak self] (resultStr) in
self?.blockContent = resultStr
})
picker.rowAndComponentCallBack = {[weak self](resultStr) in
self?.blockContent = resultStr
} self.addSubview(picker)
}else{
assert(dataSource.count != , "dataSource is not allowed to be nil")
}
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} func show(){
keyWindow?.addSubview(overlayView!)
keyWindow?.addSubview(self)
UIView.animate(withDuration: 0.25, animations: {
self.overlayView?.alpha = 1.0
var frame = self.frame
frame.origin.y = UIScreen.main.bounds.size.height - self.bounds.size.height
self.frame = frame
}) { (isFinished) in
}
} @objc func hide() {
self.dismissCallBack()
UIView.animate(withDuration: 0.25, animations: {
self.overlayView?.alpha =
var frame = self.frame
frame.origin.y = UIScreen.main.bounds.size.height
self.frame = frame
}) { (isFinished) in
self.overlayView?.removeFromSuperview()
self.removeFromSuperview()
}
} @objc func cancelAction() {
hide()
} @objc func confirmAction() {
if blockContent == "" {
showAlert(withTitle: "提示", message: "未选择任何一项!")
}else{
self.rowAndComponentCallBack!(blockContent)
}
hide()
} @objc private func showAlert(withTitle title: String?, message: String?) {
let alertVc = UIAlertController.init(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alertVc.addAction(UIAlertAction.init(title: "我知道了", style: UIAlertAction.Style.cancel, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alertVc, animated: true, completion: nil)
}
} class PickerViewBuilder : UIPickerView, UIPickerViewDelegate,UIPickerViewDataSource {
fileprivate var rowAndComponentCallBack:PopPickerViewCallBackClosure?//选择内容回调
lazy var currentSelectRow : NSInteger =
lazy var dataArr = NSMutableArray() override init(frame: CGRect) {
super.init(frame: frame)
} convenience init(frame:CGRect,dataSource:NSArray,contentCallBack:PopPickerViewCallBackClosure?) {
self.init(frame: frame)
self.backgroundColor = UIColor.white
self.dataArr = NSMutableArray.init(array: dataSource)
self.selectedRow(inComponent: )
self.delegate = self
self.dataSource = self
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} func numberOfComponents(in pickerView: UIPickerView) -> Int {
return
} func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataArr.count
} func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
//设置分割线
for view in pickerView.subviews {
if view.frame.size.height <= {
view.isHidden = false
view.frame = CGRect(x: , y: view.frame.origin.y, width: UIScreen.main.bounds.size.width, height: )
view.backgroundColor = colorWithHexString("0xe5e5e5")
}
} var pickerLabel = view as? UILabel
if pickerLabel == nil {
pickerLabel = UILabel()
pickerLabel?.textAlignment = .center
if currentSelectRow == row {
pickerLabel?.font = UIFont.systemFont(ofSize: )
pickerLabel?.textColor = newColor(, , )
}else{
pickerLabel?.font = UIFont.systemFont(ofSize: )
pickerLabel?.textColor = newColor(, , )
} }
pickerLabel?.text = dataArr[row] as? String return pickerLabel!
} func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataArr[row] as? String
} func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return
} func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
currentSelectRow = row
rowAndComponentCallBack!(dataArr[currentSelectRow] as? NSString)
self.reloadAllComponents()
}
}

使用:

let pickerView = PopPickerView.init(frame: CGRect(x: , y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width, height: ), dataSource: selectParams[indexPath.row - ] as NSArray,title:titleArr[indexPath.row])
pickerView.rowAndComponentCallBack = {(resultStr) in
print(resultStr as Any)
self.currentSelectCell?.rightSelectLabel.text = resultStr! as String
}
pickerView.show()

效果图:

【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)

  仅限于Component内容,想要多行,可自行扩展。

  自定义弹窗、可修改分割线颜色、修改字体大小、修改字体颜色。

【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)的更多相关文章

  1. Android之hint提示字体大小修改,显示完全

    Android之hint提示字体大小修改,显示完全 1.工作中遇到一个问题,就是自定义EditText的hint提示在超大字体下会显示不全, 2.然后在网上搜索了一下,在这里记录一下,分享给大家,在此 ...

  2. eclipse中文字体大小修改

    貌似有不少人苦恼eclipse中文字体大小修改问题,默认的eclipse中文字体很小,和英文字体大小完全不在一个调子上,因为默认的eclipse juno中英文字体是Consolas,字体大小是10, ...

  3. eclipse中文字体大小修改,让中英文字体协调

    貌似有不少人苦恼eclipse中文字体大小修改问题,默认的eclipse中文字体很小,和英文字体大小完全不在一个调子上,因为默认的eclipse juno中英文字体是Consolas,字体大小是10, ...

  4. Sublime Text3自定义全部字体大小、字体类型和背景颜色

    一.定义侧栏的背景颜色.字体大小和间距 Sublime Text3的Afterglow主题链接为:https://github.com/YabataDesign/afterglow-theme 1.按 ...

  5. Flash Builder 4&period;6&sol;4&period;7 注释以及字体大小修改

    ①修改字体颜色.粗体.斜体.下划线 英文版:windows-preferences-flex-editors-syntex coloring-ActionScript-Comment 汉化版:窗口—首 ...

  6. Android代码中设置字体大小,字体颜色,显示两种颜色&period;倒计时效果

    Android代码中设置字体大小,字体颜色,显示两种颜色 在xml文件中字体大小用的像素 <TextView android:id="@+id/uppaid_time" an ...

  7. css 修改placeholder字体颜色字体大小 修改input记住账号密码后的默认背景色

     壹 ❀ 引 本来这个阶段的项目页面都是给实习生妹子做的,我只用写写功能接接数据,但这两天妹子要忙翻译,这个工作阶段也快结束了导致有点慌,只能自己把剩余的几个小页面给写了. 那么做页面的过程中,UI也 ...

  8. qt 5 小练习 纯代码制作自定义按钮

    大家都知道QT设计师中直接拖动的按钮是长方形带有圆角的图案,那我们如何来设置自定义按钮呢 要设计一个按钮,我们必须要知道按钮有什么属性,首先,按钮必须有一个位置 第二,按钮必须有一个名称.还有当我们点 ...

  9. Unity 同一Text文本修改不同的字体大小和字体颜色

    类似Html,在color和size对应的<>str</>中,就能修改str的相关属性, 下面的代码就是把time改为字体颜色为红色,大小为40: 而前面的"Time ...

  10. 字体大小和背景颜色修改--Android Studio

    打开: File/seting/Editor/colors&Fonts/Fonts editor Font处默认的不让修改 所以先点击save as  随便点个名字 然后 size就可以修改了

随机推荐

  1. Windows API 设置窗口下控件Enable属性

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html http://www.yuanjiaocheng.net/we ...

  2. jq 页面延时刷新

    最常用的方法 <script language='javascript' type='text/javascript'> $(function () { setTimeout(functi ...

  3. 使用 FreeMarker 替换 JSP 的 10 个理由

    你还在使用 Java 服务器页面(俗称JSP)吗?我曾经也是,但是几年前我抛弃了它们,并且再也没有用过JSP了.JSP 是个很好的概念,但是它却剥夺了 web 开发的乐趣. 对我而言,这些都是小事,比 ...

  4. VS简单注释插件——VS插件开发续

    VS简单注释插件——VS插件开发续 前些时候,我写过一篇<VS版权信息插件——初试VS插件开发小记>分享过一个用于添加注释信息的插件,但那个插件有几个问题: 不能添加带块注释(/**/), ...

  5. GA代码中的细节

    GA-BLX交叉-Gaussion变异 中的代码细节: 我写了一个GA的代码,在2005测试函数上一直不能得到与实验室其他同学类似的数量级的结果.现在参考其他同学的代码,发现至少有如下问题: 1.在交 ...

  6. vue中的eventBus

    在vue2中,父子组件传递数据,父组件可以直接传递数据进子组件,而子组件通过调用父组件传递进来的方法,将自己的数据传递回去. 那兄弟组件之间,或者是兄弟组件的子组件之间如何传递呢? 当然vuex是一种 ...

  7. df&equals;df&period;reset&lowbar;index&lpar;drop&equals;True&rpar;

    df=df.reset_index(drop=True) ============ df = pd.read_csv('./train_file/train.csv').dropna()df_test ...

  8. Oracle创建新undo表空间最佳实践&lpar;包含段检查&rpar;

    在处理一则ORA-600 [4194]案例时,参考MOS文档:Step by step to resolve ORA-600 4194 4193 4197 on database crash (文档 ...

  9. POJ2159 Ancient Cipher

    POJ2159 Ancient Cipher Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38430   Accepted ...

  10. 第18月第19天 masonry等分 uilabel sizetofit

    1.masonry等分 mas_distributeViewsAlongAxis MASAxisTypeHorizontal 2.uilabel sizetofit +(CGSize)labSizeW ...