使用Segmented Control 利用if else變更圖片及顏色
Published in
2 min readMar 26, 2019
使用Segmented Control來做個簡單畫面
注意位置是由0開始計數的 總共有5項
先上程式碼:
@IBOutlet weak var foodImage: UIImageView!@IBAction func backGroundColor(_ sender: UISegmentedControl) {if sender.selectedSegmentIndex == 0{view.backgroundColor = .greenfoodImage.image = UIImage(named: "披薩")}else if sender.selectedSegmentIndex == 1{view.backgroundColor = .bluefoodImage.image = UIImage(named: "漢堡")}else if sender.selectedSegmentIndex == 2{view.backgroundColor = .orangefoodImage.image = UIImage(named: "牛排")}else if sender.selectedSegmentIndex == 3{view.backgroundColor = .yellowfoodImage.image = UIImage(named: "炸雞")}else if sender.selectedSegmentIndex == 4{view.backgroundColor = .blackfoodImage.image = UIImage(named: "拉麵")}}
再附動圖:
餓了嗎?