SWIFT Button的基本用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SWIFT Button的基本用法相关的知识,希望对你有一定的参考价值。
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) } }
以上是关于SWIFT Button的基本用法的主要内容,如果未能解决你的问题,请参考以下文章
swift button的基本属性设置与不显示title只显示边框和背景色的原因