在iOS开发中,自定义提示框是提高用户体验和界面美观性的重要手段。Swift3作为iOS开发的主要编程语言,提供了丰富的功能来帮助开发者创建个性化的自定义提示框。本文将详细揭秘如何在Swift3中轻松打造个性化自定义提示框。
1. 理解自定义提示框
自定义提示框,顾名思义,是指开发者根据需求,自定义样式和内容的提示框。在Swift3中,我们可以通过多种方式来实现,例如使用UIKit框架中的UIAlertView、UIView以及自定义的ViewController。
2. 使用UIAlertView创建自定义提示框
UIAlertView是UIKit框架中提供的一个简单的方式来创建提示框。以下是一个使用UIAlertView创建自定义提示框的示例代码:
import UIKit
func showAlert() {
let alertController = UIAlertController(title: "提示", message: "这是一个自定义提示框", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) in
print("点击了确定")
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
在这个示例中,我们创建了一个包含标题、消息和两个按钮(取消和确定)的提示框。
3. 使用UIView创建自定义提示框
除了使用UIAlertView,我们还可以通过自定义UIView来创建更加个性化的提示框。以下是一个使用UIView创建自定义提示框的示例代码:
import UIKit
class CustomAlertView: UIView {
let titleLab = UILabel()
let messageLab = UILabel()
let cancelBtn = UIButton()
let okBtn = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupUI()
}
func setupUI() {
// 设置标题和消息
titleLab.text = "提示"
titleLab.font = UIFont.systemFont(ofSize: 18)
titleLab.textAlignment = .center
self.addSubview(titleLab)
messageLab.text = "这是一个自定义提示框"
messageLab.font = UIFont.systemFont(ofSize: 14)
messageLab.textAlignment = .center
self.addSubview(messageLab)
// 设置按钮
cancelBtn.setTitle("取消", for: .normal)
cancelBtn.setTitleColor(UIColor.blue, for: .normal)
cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
self.addSubview(cancelBtn)
okBtn.setTitle("确定", for: .normal)
okBtn.setTitleColor(UIColor.blue, for: .normal)
okBtn.addTarget(self, action: #selector(okAction), for: .touchUpInside)
self.addSubview(okBtn)
// 设置布局
titleLab.snp.makeConstraints { (make) in
make.top.equalTo(self).offset(20)
make.centerX.equalTo(self)
}
messageLab.snp.makeConstraints { (make) in
make.top.equalTo(titleLab.snp.bottom).offset(10)
make.centerX.equalTo(self)
make.width.equalTo(200)
}
cancelBtn.snp.makeConstraints { (make) in
make.top.equalTo(messageLab.snp.bottom).offset(20)
make.left.equalTo(self).offset(10)
}
okBtn.snp.makeConstraints { (make) in
make.top.equalTo(messageLab.snp.bottom).offset(20)
make.right.equalTo(self).offset(-10)
}
}
@objc func cancelAction() {
self.removeFromSuperview()
}
@objc func okAction() {
self.removeFromSuperview()
}
}
在这个示例中,我们创建了一个自定义的UIView,并在其中添加了标题、消息和两个按钮。通过调整布局和样式,我们可以创建出更加个性化的提示框。
4. 使用ViewController创建自定义提示框
除了上述两种方法,我们还可以通过自定义ViewController来创建自定义提示框。以下是一个使用ViewController创建自定义提示框的示例代码:
import UIKit
class CustomAlertViewController: UIViewController {
let titleLab = UILabel()
let messageLab = UILabel()
let cancelBtn = UIButton()
let okBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
// 设置标题和消息
titleLab.text = "提示"
titleLab.font = UIFont.systemFont(ofSize: 18)
titleLab.textAlignment = .center
view.addSubview(titleLab)
messageLab.text = "这是一个自定义提示框"
messageLab.font = UIFont.systemFont(ofSize: 14)
messageLab.textAlignment = .center
view.addSubview(messageLab)
// 设置按钮
cancelBtn.setTitle("取消", for: .normal)
cancelBtn.setTitleColor(UIColor.blue, for: .normal)
cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
view.addSubview(cancelBtn)
okBtn.setTitle("确定", for: .normal)
okBtn.setTitleColor(UIColor.blue, for: .normal)
okBtn.addTarget(self, action: #selector(okAction), for: .touchUpInside)
view.addSubview(okBtn)
// 设置布局
titleLab.snp.makeConstraints { (make) in
make.top.equalTo(view).offset(20)
make.centerX.equalTo(view)
}
messageLab.snp.makeConstraints { (make) in
make.top.equalTo(titleLab.snp.bottom).offset(10)
make.centerX.equalTo(view)
make.width.equalTo(200)
}
cancelBtn.snp.makeConstraints { (make) in
make.top.equalTo(messageLab.snp.bottom).offset(20)
make.left.equalTo(view).offset(10)
}
okBtn.snp.makeConstraints { (make) in
make.top.equalTo(messageLab.snp.bottom).offset(20)
make.right.equalTo(view).offset(-10)
}
}
@objc func cancelAction() {
dismiss(animated: true, completion: nil)
}
@objc func okAction() {
dismiss(animated: true, completion: nil)
}
}
在这个示例中,我们创建了一个自定义的ViewController,并在其中添加了标题、消息和两个按钮。通过调整布局和样式,我们可以创建出更加个性化的提示框。
5. 总结
通过以上介绍,我们可以看到在Swift3中,开发者可以根据需求选择使用UIAlertView、UIView或ViewController来创建个性化的自定义提示框。这些方法各有特点,开发者可以根据自己的实际情况选择合适的方法。在实际开发中,我们可以根据需求调整提示框的样式和内容,从而提升用户体验和界面美观性。
