LoginSignup
118
121

More than 5 years have passed since last update.

UIButtonを動的に作るサンプルコード

Posted at

ほとんどのアプリで必ず使われるUIButtonですが、swiftでの基本的な使い方を見て行きたいと思います。

let button = UIButton()
//表示されるテキスト
button.setTitle("Tap Me!", forState: .Normal)

//テキストの色
button.setTitleColor(UIColor.blueColor(), forState: .Normal)

//タップした状態のテキスト
button.setTitle("Tapped!", forState: .Highlighted)

//タップした状態の色
button.setTitleColor(UIColor.redColor(), forState: .Highlighted)

//サイズ
button.frame = CGRectMake(0, 0, 300, 50)

//タグ番号
button.tag = 1

//配置場所
button.layer.position = CGPoint(x: self.view.frame.width/2, y:100)

//背景色
button.backgroundColor = UIColor(red: 0.7, green: 0.2, blue: 0.2, alpha: 0.2)

//角丸
button.layer.cornerRadius = 10

//ボーダー幅
button.layer.borderWidth = 1

//ボタンをタップした時に実行するメソッドを指定
button.addTarget(self, action: "tapped:", forControlEvents:.TouchUpInside)

//viewにボタンを追加する
self.view.addSubview(button)

サンプルコード、詳細の解説はこちらに
http://tech.eversense.co.jp/38

118
121
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
118
121