3, Swift 中弹窗的应用UIAlertController

在应用中有很多的弹窗坑定是需要的,这里随便写了几个系统的UIAlertController的简单应用,主要有四个

1,常规弹窗,确定按钮,取消按钮,或者只有确定,或者取消按钮;

2,选择图片的从底部滑动的弹窗;

3,带输入框的弹窗

4,一闪而过的弹框,但是这种样式写死,没法改变背景颜色等等这些东西;

代码如下

import UIKit


class AlertController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let button = UIButton();
        button.frame = CGRect.init(x: 20, y: 100, width: UIScreen.main.bounds.width - 2*20, height: 56);
        button .titleLabel?.font = UIFont.systemFont(ofSize: 16);
        button.setTitle("确定取消只展示消息", for: UIControlState.normal)
        button .setTitleColor(UIColor .red, for: UIControlState.normal);
        
        button .backgroundColor = UIColor.orange;
        button.addTarget(self, action: #selector(alertTypeNormal), for: UIControlEvents.touchUpInside)
        self.view .addSubview(button);
        
        let sheetBtn = UIButton();
        sheetBtn.frame = CGRect.init(x: 20, y: 156+20 , width: UIScreen.main.bounds.width - 2*20, height: 56);
        sheetBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
        sheetBtn.setTitle("底部滑出", for: UIControlState.normal)
        sheetBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
        
        sheetBtn .backgroundColor = UIColor.gray;
        sheetBtn.addTarget(self, action: #selector(alertSheet), for: UIControlEvents.touchUpInside)
        self.view .addSubview(sheetBtn);
        
        let logBtn = UIButton();
        logBtn.frame = CGRect.init(x: 20, y: 156+20 + 76 , width: UIScreen.main.bounds.width - 2*20, height: 56);
        logBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
        logBtn.setTitle("带有textField控件的", for: UIControlState.normal)
        logBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
        
        logBtn .backgroundColor = UIColor.green;
        logBtn.addTarget(self, action: #selector(alertTF), for: UIControlEvents.touchUpInside)
        self.view .addSubview(logBtn);
        
        let disBtn = UIButton();
        disBtn.frame = CGRect.init(x: 20, y: 156+20 + 76 + 76 , width: UIScreen.main.bounds.width - 2*20, height: 56);
        disBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
        disBtn.setTitle("一闪而过的效果", for: UIControlState.normal)
        disBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
        
        disBtn .backgroundColor = UIColor.brown;
        disBtn.addTarget(self, action: #selector(alertDismiss), for: UIControlEvents.touchUpInside)
        self.view .addSubview(disBtn);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /**一般弹窗类型 有确定和取消,可以只有一个按钮的将action修改就可以了
     destructive  使用此风格 字体颜色为红色 其他类型位系统默认的蓝色**/
    func alertTypeNormal(){
        let alertNormal = UIAlertController(title: "系统提示", message: "无上的神伟大的小明", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let sureAction = UIAlertAction(title: "确定", style: .destructive) { (UIAlertAction) in
            print("点击了确定")
        }
        alertNormal.addAction(cancelAction);
        alertNormal.addAction(sureAction);
        self.present(alertNormal, animated: true) {
            
        }
    }
    /**一般用于选择图片或者拍照的弹框从底部滑出**/
    func alertSheet(){
        let alertsheet = UIAlertController(title: "请选择图片或拍摄图片", message: "", preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) in
            print("选择了取消");

        })
        let  photoAction = UIAlertAction(title: "相机", style: .default) { (UIAlertAction) in
            print("选择了相机");
        }
        let tumbAction = UIAlertAction(title: "相册", style: .default) { (UIAlertAction) in
            print("选择了相册");
        }
        alertsheet.addAction(cancelAction);
        alertsheet.addAction(photoAction);
        alertsheet.addAction(tumbAction);
        
        self.present(alertsheet, animated: true) {
            
        }
    }
    /**带有输入框的弹窗**/
    func alertTF(){
        let alertController = UIAlertController(title: "登录页面",message: "输入用户名和密码", preferredStyle: .alert)
        alertController.addTextField {
            (textField: UITextField!) -> Void in
            textField.placeholder = "请输入用户名"
        }
        alertController.addTextField {
            (textField: UITextField!) -> Void in
            textField.placeholder = "请输入密码"
            textField.isSecureTextEntry = true
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "登录", style: .default, handler: {
            action in
            //也可以用下标的形式获取textField let login = alertController.textFields![0]
            let login = alertController.textFields!.first!
            let password = alertController.textFields!.last!
            print("用户名:\(login.text) 密码:\(password.text)")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    /**规定时间后消失的弹框**/
    func alertDismiss(){
    let alertController = UIAlertController(title: "保存成功!",message: nil, preferredStyle: .alert)
        
        //显示提示框
        self.present(alertController, animated: true, completion: nil)
                    //两秒钟后自动消失
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
        self.presentedViewController?.dismiss(animated: false, completion: nil)
        
        }

    
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值