标题本身解释尽可能多。基本上,我有一个分段控件,我想在每个段被选中后更改我的图像视图中的图像,但我似乎无法应用或找到解决我的问题的逻辑。答案本身可能非常简单,但我对Swift来说真的很新,并且无法在任何地方找到答案,所以我希望能解决我的问题。谢谢!
@IBOutlet weak var newImageView: UIImageView!
@IBAction func chooseImage(sender: AnyObject) {
if myPhotoSegment.selectedSegmentIndex == 0
{
newImageView = UIImage(named: "3.jpg")
}
if myPhotoSegment.selectedSegmentIndex == 1
{
}
答案 0 :(得分:3)
您无法将UIImageView的值设置为UIImage。 改为设置newImageView.image的值。这也是switch语句的好机会。
@IBAction func chooseImage(sender: AnyObject) {
switch myPhotoSegment.selectedSegmentIndex {
case 0:
newImageView.image = UIImage(named: "3.jpg")
case 1:
newImageView.image = UIImage(named: "4.jpg")
default:
newImageView.image = nil
}
}
答案 1 :(得分:0)
在swift4中在imageview中使用图像进行细分
@IBOutlet weak var bgimage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red
let itemsimg = ["google", "facebook", "instagram" , "twitter"]
let customSI = UISegmentedControl(items: itemsimg)
customSI.selectedSegmentIndex = 0
customSI.frame = CGRect(x:50, y:100, width:300, height: 40)
customSI.layer.cornerRadius = 5.0
customSI.backgroundColor = UIColor.black
customSI.tintColor = UIColor.white
customSI.addTarget(self, action: #selector(changeimg),for: .valueChanged)
self.view.addSubview(customSI)
}
@objc func changeimg(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 1:
bgimage.image = UIImage(named: "facebook")
case 2:
bgimage.image = UIImage(named: "instagram")
case 3:
bgimage.image = UIImage(named: "twitter")
default:
bgimage.image = UIImage(named: "google")
}
}
}