适宜有一定ios开发基础人群
1.打开xcode
2.点击导航栏的file----new----project
3.选择single View App
4.填入Product Name,在language那一栏选择Swift,下一步
5.此时你的项目中有两个后缀为.Swift的文件(Appdelegate ViewController)
6.点击Viewcontroller.swift文件,在Viewdidload接口中创建你的按钮
直接上代码
let newButton:UIButton = UIButton(frame: CGRect(x: 0, y: 20, width: 50, height: 50))
newButton.backgroundColor = UIColor.blue
newButton.setTitle("点我", for: .normal)
newButton.addTarget(self, action: #selector(newButtonAction), for: .touchUpInside)
self.view.addSubview(newButton)
点击事件
@objc func newButtonAction() {
print("select new button")
}