0122iosapp_设置弹出警告提示,输入框左侧加入图片,重温代码加入图片

项目 map0122

//
//  ViewController.swift
//  map0122
//
//  Created by Mac on 1/22/19.
//  Copyright © 2019 Mac. All rights reserved.
//

import UIKit
//本项目实现效果;1,体统的警告提示;2,输入框左侧使用图片站位

class ViewController: UIViewController {
    
  
    @IBOutlet weak var input_TextField: UITextField!
    
    
    @IBAction func up_Button(_ sender: UIButton) {
        
        if input_TextField.text == "" {//文本框空为真
            //执行警告提示
            outAlert(title: "输入框为空", message: "请输入内容")
            
        }else{
            outAlert(title: "已输入内容", message: input_TextField.text!)
        }
    }
    
    //创建弹出提示的方法
    func outAlert(title:String,message:String) {
        //实例化系统提示窗口对象
        let alert_controller = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        //实例化在提示窗口的内的动作选项对象
        let alert_action = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
        //将提示窗口内的动作选项对象 加 到提示窗口对象上
        alert_controller.addAction(alert_action)
    
        //将提示窗加到view视图上
        self.present(alert_controller, animated: true, completion: nil)
    }
    
    //创建输入框左侧用图片站位的方法
    func setTextFieldLeftImage(text_field: UITextField,imageName:String) {
        //将输入框的左测视图模式打开,这样就能进行编辑了
        text_field.leftViewMode = UITextField.ViewMode.always
        //设置图片视图框,用来存放图片
        let icon_image_view = UIImageView()
        //将图片放入图片视图
        icon_image_view.image = UIImage(named: imageName)
        //设置视图框的框架位置和尺寸
        icon_image_view.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        //设置图片内容模式,让图片不要超过图片视图框
        icon_image_view.contentMode = UIView.ContentMode.scaleAspectFit
        //将图片视图 赋值到 输入框的左侧视图 注意输入框的左侧视图必须存在
        text_field.leftView = icon_image_view
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        setTextFieldLeftImage(text_field: input_TextField, imageName: "jiantou0115")
   
    }


}

猜你喜欢

转载自blog.csdn.net/whqwjb/article/details/86609006
今日推荐