Swift-创建UIButton(其他UI组件雷同)
let button = UIButton.init(frame: CGRectMake(100, 100, 100, 100)) button.setTitle("按钮", forState: UIControlState.Normal) button.backgroundColor = UIColor.redColor() button.addTarget(self, action:#selector(buttonClick), forControlEvents: UIControlEvents.TouchUpInside) button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal) self.view.addSubview(button) //点击事件 func buttonClick(){ print("点击") }