I just finished Project2 but when I added the borders to the flag buttons they don't outline the flag images perfectly. It seems like there is some padding between the image and the border. I tried to look through the Attribute Inspector for the buttons to see if I could change it there, but I'm not seeing it.
How can I remove the extra space between the button image and the border?
My code looks like this if it helps at all
import UIKit
class ViewController: UIViewController {
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
var countries = [String]()
var score = 0
var correctAnswer = 0
var currentQuestion = 1
override func viewDidLoad() {
super.viewDidLoad()
countries += ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"]
button1.layer.borderWidth = 1
button2.layer.borderWidth = 1
button3.layer.borderWidth = 1
button1.layer.borderColor = UIColor.lightGray.cgColor
button2.layer.borderColor = UIColor.lightGray.cgColor
button3.layer.borderColor = UIColor.lightGray.cgColor
askQuestion()
}
func askQuestion(action: UIAlertAction! = nil) {
countries.shuffle()
correctAnswer = Int.random(in: 0...2)
title = "\(countries[correctAnswer].uppercased()) : \(score)"
button1.setImage(UIImage(named: countries[0]), for: .normal)
button2.setImage(UIImage(named: countries[1]), for: .normal)
button3.setImage(UIImage(named: countries[2]), for: .normal)
}
func newGame(action: UIAlertAction! = nil) {
score = 0
currentQuestion = 1
askQuestion()
}
@IBAction func buttonTapped(_ sender: UIButton) {
if sender.tag == correctAnswer {
title = "Correct"
score += 1
} else {
title = "Wrong"
score -= 1
}
var alertActionTitle = "Continue"
var alertMessage = "That is the flag of \(countries[sender.tag].uppercased())\nQuestion: \(currentQuestion)/10\nYour score is: \(score)"
var alertHandler = askQuestion
if currentQuestion < 10 {
currentQuestion += 1
} else {
alertActionTitle = "New Game"
alertMessage = "Game Over\nFinal score: \(score)"
alertHandler = newGame
}
let ac = UIAlertController(title: title, message: alertMessage, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: alertActionTitle, style: .default, handler: alertHandler))
present(ac, animated: true)
}
}