SWIFT Button的基本用法
2016-02-22 20:51
471 查看
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // Override point for customization after application launch. self.window!.backgroundColor = UIColor.whiteColor() self.window!.rootViewController = RootViewController() self.window!.makeKeyAndVisible() return true } }
import UIKit class RootViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //初始化button let btn = UIButton(type: UIButtonType.Custom) //尺寸 btn.frame = CGRect(x: 100, y: 100, width: 100, height: 50) //背景颜色 btn.backgroundColor = UIColor.redColor() //设置标题 btn.setTitle("按钮", forState: UIControlState.Normal) //设置标题的颜色 btn.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal) //添加点击事件 btn.addTarget(self, action: "printSomeThing:", forControlEvents: UIControlEvents.TouchUpInside) //self.view加载子视图btn self.view.addSubview(btn) } //点击按钮执行的方法 func printSomeThing(button:UIButton){ print(button) } }
相关文章推荐
- The Swift Programming Language学习笔记(十一)——属性
- The Swift Programming Language学习笔记(十)——类和结构体
- The Swift Programming Language学习笔记(九)——枚举
- The Swift Programming Language学习笔记(八)——闭包
- Swift UI学习之UITableView and protocol use
- Swift基础语法(一)
- Swift - 访问控制(private,internal,public)
- Swift学习-逻辑分支
- Swift UI学习之基础:UIButton,UILabel and UISlider
- Swift UIWebView的使用
- Swift-常量与变量
- 最近学swift的一些记录
- 窥探Swift之数组安全索引与数组切片
- swift 字符串——swift 学习(一)
- Library not loaded: @rpath/libswiftCore.dylib的解决方法
- Swift中计算字符串长度
- swift基本语法(总结提炼版)之003 Swift类型转换
- swift基本语法(总结提炼版)之002逻辑分支
- swift基本语法(总结提炼版)之001常量&变量
- Swift基础语法&&入门教程_字符串,数组_字典