UIButton(ボタン)
【ボタンをコードで作る】
Storyboardを使わずに、コードでボタンを作成します。 //インスタンスの生成 let Button = UIButton() //ボタンの大きさ Button.frame = CGRect(x:screenW/2, y: screenH/2, width:screenW/2, height: screenH/2) //ボタンに背景色をつける Button.backgroundColor = UIColor.white //ボタンに配置するイメージの生成 let ButtonImage = UIImage(named: "ファイル名") //ボタンにイメージを配置 Button.setImage(ButtonImage!, for: .normal) //ボタンに表示するタイトル Button.setTitle("", for: UIControl.State.normal) //ボタンに表示するタイトルの色 Button.setTitleColor(UIColor.gray, for: .normal) //ボタンに表示するタイトルのフォントサイズ Button.titleLabel?.font = UIFont.systemFont(ofSize: 35) //ボタン押した時のメソッド Button.addTarget(self, action: #selector(ViewController. Buttontap(_:)), for: .touchUpInside) //ボタンを押したときの動作 @IBAction func Buttontap(_ sender: Any) { } |