Reputation: 7
i use toolbar items for using action properties, i want when i tapped on on of the items, images be change to highlighted image.
this is my all three buttons :
@IBAction func audio(sender: AnyObject) {
self.scView.contentOffset = CGPointMake(0,0);
}
@IBAction func mainBtn(sender: AnyObject) {
self.scView.contentOffset = CGPointMake(415,0);
}
@IBAction func videoBtn(sender: AnyObject) {
self.scView.contentOffset = CGPointMake(830,0);
}
@IBOutlet var inpt: UITextField!
@IBOutlet var scView: UIScrollView!
this is my app picture: image
thanks
Upvotes: 0
Views: 783
Reputation: 5554
If all you want is to show the button with a highlighted images while you are pressing the button - you can do that through the storyboard - you can set the image for each button in the default
state and also in highlighted
- but when you release the button, it goes back to the original image.
If you want the image to toggle when you select the button, you're going to have to add outlets for the buttons, and a button state variable (if it only changes once, and never goes back to the original, then you don't need the state)
Here's an example of how you might do it - updated to reset the 'other' button
@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var videoButton: UIButton!
var mainButtonSelected : Bool = false
var videoButtonSelected : Bool = false
@IBAction func mainButton(sender: AnyObject)
{
mainButtonSelected = !mainButtonSelected // toggle state
if mainButtonSelected
{
// highlight the mainButton
mainButton.setImage(UIImage(named: "imageMainHighlight.png"), forState: .Normal)
// clear the highlight (if any) on videoButton
videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)
// UPDATED
// make sure the videoButtonSelected flag is correct
videoButtonSelected = false
// UPDATED
}
else
{
// clear the highlight on mainButton, no need to do anything with videoButton
mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)
}
// add any other code you need here
}
@IBAction func videoButton(sender: AnyObject)
{
videoButtonSelected = !videoButtonSelected // toggle state
if videoButtonSelected
{
// highlight videoButton
videoButton.setImage(UIImage(named: "imageVideoHighlight.png"), forState: .Normal)
// clear the highlight (if any) on mainButton
mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)
// UPDATED
// make sure the mainButtonSelected flag is correct
mainButtonSelected = false
// UPDATED
}
else
{
// clear the highlight on videoButton, no need to do anything for mainButton
videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)
}
// add any other code you need here
}
Upvotes: 1