关于Swift 动态生成按钮

关于Swift 动态生成按钮

//
//  FirstViewController.swift
//  esb
//
//  Created by jiangzhihao on 2018/11/12.
//  Copyright © 2018年 jiangzhihao. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        for i in 0 ..< 3  {
            let btnLogin = UIButton(type: .roundedRect)
            btnLogin.frame = CGRect(x: 10 + i * 125, y: 200, width: 120, height: 50)
            btnLogin.setTitle("按钮演示", for: .normal)
            btnLogin.setTitleColor(.black, for: .normal)
            btnLogin.titleLabel?.font = UIFont.systemFont(ofSize: 18)
            btnLogin.layer.cornerRadius = 5
            btnLogin.layer.borderWidth = 1
            btnLogin.layer.borderColor = UIColor.black.cgColor
            btnLogin.tag = i
            btnLogin.addTarget(self, action: #selector(tapped_login(_:)), for: .touchUpInside)
            self.view.addSubview(btnLogin)
        }
    }

    @objc func tapped_login(_ button:UIButton) {
        print("ok:\(button.tag)")
    }

}

 

发表回复

您的电子邮箱地址不会被公开。