版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/84069879
//UIButton的使用
func test1() {
//创建一个深色背景的提示按钮
let button1=UIButton(type: UIButtonType.infoDark)
//设置按钮位置大小
button1.frame=CGRect(x: 130, y: 80, width: 40, height: 40);
//创建一个圆角的按钮
let button2=UIButton(type: UIButtonType.roundedRect);
//设置按钮位置大小
button2.frame=CGRect(x: 80, y: 180, width: 150, height: 40);
//设置背景和前景色
button2.backgroundColor=UIColor.purple
button2.tintColor=UIColor.yellow;
button2.setTitle("button2", for: UIControlState());
button2.addTarget(self, action: #selector(ViewController.button2OnClick(_ :)), for: UIControlEvents.touchUpInside)
//创建一个圆角按钮
let button3=UIButton(type: UIButtonType.roundedRect);
//设置背景色 前景色
button3.backgroundColor=UIColor.brown;
button3.tintColor=UIColor.white;
//设置标题
button3.setTitle("button3", for: UIControlState());
button3.frame=CGRect(x: 80, y: 280, width: 150, height: 40)
//设置边框
button3.layer.masksToBounds=true;
//设置按钮层圆角半径为10
button3.layer.cornerRadius=10
//设置边框宽度为4
button3.layer.borderWidth=4
//设置边框颜色
button3.layer.backgroundColor=UIColor.lightGray.cgColor;
self.view.addSubview(button1);
self.view.addSubview(button2);
self.view.addSubview(button3);
//创建一个圆角按钮
let imgButton = UIButton(type: UIButtonType.roundedRect);
//设置大小
imgButton.frame=CGRect(x: 31, y: 530, width: 257, height: 60);
//设置图片背景
imgButton.setBackgroundImage(UIImage(named: "Sample"), for: UIControlState())
//设置标题
imgButton.setTitle("imgButton", for: UIControlState())
//字体
imgButton.titleLabel?.font=UIFont(name: "Arial", size: 24)
//颜色
imgButton.setTitleColor(UIColor.red, for: UIControlState());
imgButton.addTarget(self, action: #selector(ViewController.button2OnClick(_:)), for: UIControlEvents.touchUpInside);
self.view.addSubview(imgButton);
}
@objc func button2OnClick(_ sender:UIButton){
let dialog=UIAlertController(title: "title", message: "content", preferredStyle: UIAlertControllerStyle.alert)
let dialogAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil);
dialog.addAction(dialogAction)
self.present(dialog,animated: true,completion: nil);
}