How to make UIBarButton not greyed out when disabled? - ios

I have a UIBarButton saveBarButton to save an image of the screen to the camera roll. When the user presses the saveBarButton the image is saved and the UIBarButton saveBarButton changes to the UIImage doneIconfor 1.2 seconds to indicate to the user that the image has been saved. When the saveBarButton displays the doneIcon I disable saveBarButton. However, when I do that the saveBarButton is greyed out.
My question is: how do I stop the UIBarButton from greying out when disabled?
My code:
//create UIBarButton saveBarButton
override func viewDidLoad() {
let saveBarButton = UIBarButtonItem(image: UIImage(named: "saveIcon"), style: .Plain, target: self, action: "save:")
saveBarButton.tintColor = colorGreyDark
}
//save function called when press saveBarButton
func save(sender: UIBarButtonItem) {
//save image
deselectShape()
let window: UIWindow! = UIApplication.sharedApplication().keyWindow
let windowImage = capture(window)
UIImageWriteToSavedPhotosAlbum(windowImage
, nil, nil, nil)
//Change saveBarButton to indicate to user that image was saved
sender.image = UIImage(named: "doneIcon")
sender.enabled = false //disable saveBarButton
self.performSelector("canSaveAgain:", withObject: sender, afterDelay: 1.2)
}
//Change saveBarButton to original icon to indicate to user that can save another image
func canSaveAgain(sender: UIBarButtonItem){
sender.image = UIImage(named: "saveIcon")
sender.enabled = true //enable saveBarButton
}
To see what I'm talking about.

One of ways is to create UIBarButtonItem with customView and use userInteractionEnabled instead enabled.
let saveBarButton = UIBarButtonItem(customView: saveButton)
saveBarButton.customView?.userInteractionEnabled = false

Related

How to adjust the opacity of a highlighted image in Xcode?

I have added a tap gesture to my UIImageView. So, whenever the user taps the image, an opaque highlighted image should appear. Could anyone let me know how can I achieve that? Thanks for the help!
override func viewDidLoad()
{
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
#objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
tappedImage.isHighlighted = true
}
I can see the highlighted image, after tapping the ImageView, But, how can I decrease its opacity (alpha value) of the highlighted image! Would appreciate your help! Thanks!

UIBarButtonItem position different when placed via storyboard vs. programmatically

In my app, I have a toolbar with UIBarButtonItems.
In most circumstances, the UIBarButtonItems are set via storyboard, and look as follows:
In a special case, I have to replace one UIBarButtonItem programmatically. This is done with the following code:
let rotatingButton = UIButton(type: .custom)
rotatingButton.setImage(UIImage(named: "LocalizationInUseNoFix"), for: .normal)
rotatingButton.addTarget(self, action: #selector(localizationButtonTapped), for: .touchUpInside)
rotatingButton.rotateStart()
let barButtonItem = UIBarButtonItem(customView: rotatingButton)
leftBarButtonItems![2] = barButtonItem
When the rotatingButton is displayed in the toolbar, it placed at a different position. It is shifted to the right, as you can see here:
How can I achieve to place both UIBarButtonItems at the same position?
EDIT:
By now I realized that the horizontal shift of the programmatically created UIBarButtonItem is not always the same, without any changes to the code: Sometimes it is shifted left, and not right:
EDIT 2:
I found a workaround:
If I set a width constrain to my button like
rotatingButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
then the button is apparently always correctly placed. But I hate to hard-code constraints like this.
Is there a more elegant way to do it?
Try the below steps to perform your task:
Store left bar button items into an NSMutableArray
Replace desired UIBarbuttonItem
Set leftbarbuttonitems to this new array
Hope this steps will work
When you set the image on UIBarButton programmatically, the contentmode of the leftBarButtonItems becomes 'left' and rightBarButtonItems become 'right'. But from storyboard, it is centered. Set the image and adjust the contentMode as required.
All are working fine for Navigationbar and Toolbar
class ViewController: UIViewController {
#IBOutlet weak var toolbar: UIToolbar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func leftAction(_ sender: Any) {
}
#IBAction func rightAction(_ sender: Any) {
}
#IBAction func changeLeftItems(_ sender: Any) {
if let items = self.navigationItem.leftBarButtonItems {
var addItems = [UIBarButtonItem]()
addItems.append(contentsOf: items)
let barItem = UIBarButtonItem(title: "3", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.leftAction(_:)))
addItems.append(barItem)
self.navigationItem.leftBarButtonItems = addItems
}
if let items = self.toolbar.items {
var addItems = [UIBarButtonItem]()
addItems.append(contentsOf: items)
let barItem = UIBarButtonItem(title: "L3", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.leftAction(_:)))
addItems.insert(barItem, at: 2)
self.toolbar.setItems(addItems, animated: true)
}
}
}
This is the best solution I found so far:
Get the width of a view of another bar button item using key value coding. This is from Jeremy W. Sherman’s answer here.
Please note that it does not use any private API, see the discussion there. The worst thing that can happen is that the view property of the UIBarButtonItem cannot be accessed. In this case, I use a default value:
var leftBarButtonItems = self.navigationItem.leftBarButtonItems
let rotatingButton = UIButton(type: .custom)
rotatingButton.setImage(UIImage(named: "LocalizationInUseNoFix"), for: .normal)
rotatingButton.addTarget(self, action: #selector(localizationButtonTapped), for: .touchUpInside)
rotatingButton.rotateStart()
// Get the width of the bar button items if possible, else set default
let leftmostBarButtonItem = leftBarButtonItems![0]
let barButtonItemWidth: CGFloat
if let leftmostBarButtonItemView = leftmostBarButtonItem.value(forKey: "view") as? UIView {
barButtonItemWidth = leftmostBarButtonItemView.frame.size.width
} else {
barButtonItemWidth = 40.0
}
rotatingButton.widthAnchor.constraint(equalToConstant: barButtonItemWidth).isActive = true
let barButtonItem = UIBarButtonItem(customView: rotatingButton)
leftBarButtonItems![2] = barButtonItem
self.navigationItem.leftBarButtonItems = leftBarButtonItems
This is working fine for me. Best way is identify item to replace and change the content
#IBAction func changeLeftItems(_ sender: Any) {
if let items = self.toolbar.items {
var addItems = [UIBarButtonItem]()
addItems.append(contentsOf: items)
let barItem = UIBarButtonItem(title: "L5", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.leftAction(_:)))
addItems.remove(at: 1)
addItems.insert(barItem, at: 1)
self.toolbar.setItems(addItems, animated: true)
}
}

Trying to make rightBarButton appear after making it disappear using Swift

I currently am working on an app in Swift where in my viewDidLoad() method I have purposely hidden my rightBarButton on my navigation bar like this:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(TableViewController.dismiss))
self.navigationItem.setRightBarButtonItem(nil, animated: true)
However, under certain circumstances, I would like to display the rightBarButton. How would I do this? What would be the opposite of the above line of code?
Once you set the bar button item to nil, it is gone. Something you can do however, is store the bar button item like so:
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(TableViewController.dismiss));
and then you can make it appear/disappear like so:
self.navigationItem.rightBarButtonItem = barButtonItem
self.navigationItem.setRightBarButtonItem(nil, animated: true)
then just access the barButtonItem whenever you want it to appear/disappear.
You can do one of the following two options:
Keep a reference of your UIBarButtonItem and every time you disappear you save it to then when you want to show it again you set the old value.
Play with the color of the UIBarButtonItem and the enabled/disable property to enable the interaction with it.
The first choice always keep a reference globally to the UIBarButtonItem and the second need to know the exact color of the original UIBarButtonItem to give to its original state:
First Option:
private var isHidden: Bool!
private var righBarButtonItem: UIBarButtonItem!
#IBAction func hideButton(sender: AnyObject) {
if self.isHidden == true {
self.isHidden = false
self.navigationItem.rightBarButtonItem = righBarButtonItem
}
else {
self.isHidden = true
righBarButtonItem = self.navigationItem.rightBarButtonItem
self.navigationItem.setRightBarButtonItem(nil, animated: true)
}
}
Second Option:
#IBAction func hideButton(sender: AnyObject) {
if self.isHidden == true {
self.isHidden = false
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clearColor()
self.navigationItem.rightBarButtonItem?.enabled = false
}
else {
self.isHidden = true
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.blueColor()
self.navigationItem.rightBarButtonItem?.enabled = true
}
}
In the above examples I set a variable with the state of the UIBarButtonItem for purposes of know the value and and #IBOutlet to hide/show the UIBarButtonItem. The variable isHidden need to set it's initial value in the viewDidLoad.
I hope this help you.

UIBarbutton 'Save' not appearing again after UIActivity Indicator stops on navigation Bar

There is a Save (System Item) on my navigation bar as BarButtonItem I am showing UIActivityIndicatorView on the navigation bar when user clicks this Save Button and I want to appear this Barbutton(Save) again on certain condition. First I think the problem is I am adding a indicator on customView so I don't need to hide the barbutton.It automatically hides itself after I start the indicator. But don't know now how to show Save Button again. or how can I remove the indicator from customView
This is how I am doing
#IBOutlet weak var saveButtonOutlet: UIBarButtonItem!
var activityIndicatorView:UIActivityIndicatorView!
func showActivityIndicator() {
activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicatorView.frame = CGRectMake(0, 0, 14, 14)
activityIndicatorView.color = UIColor().blueColorIOS()
activityIndicatorView.startAnimating()
let barButtonItem = UIBarButtonItem(customView: activityIndicatorView)
self.navigationItem.rightBarButtonItem = barButtonItem
}
#IBAction func saveButtonClicked(sender: UIBarButtonItem) {
showActivityIndicator()
ServerRequest.postToServer(url, params: params){
result, error in
if let result = result {
let code = result["code"] as? Int
print(result)
if (code==200){
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { ()->() in
self.activityIndicatorView.hidden = true
self.activityIndicatorView.hidesWhenStopped = true
//here want to show again "saveButtonOutlet"
})
}
}
}
}
}
So one way to do this is to create the Save button again, and setting the rightBarButtonItem again:
...
self.activityIndicatorView.hidden = true
self.activityIndicatorView.hidesWhenStopped = true
let barButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Save, target: self, action: "saveButtonClicked:")
self.navigationItem.rightBarButtonItem = barButtonItem
And I'd also replace self.activityIndicatorView.hidden = true with self.activityIndicatorView.stopAnimating() to properly use the hidesWhenStopped property.
I think all you need to do is reset the self.navigationItem.rightBarButtonItem to the saveButtonOutlet.
Worked right now for me.

How do I change the image of a button when tapped - Swift

I’m trying to change the image of a button when tapped (it’s for an audio player that I want Play to change to Pause). So far I’ve only managed to change it when the button is held, then it changes back when released.
I’ve tried using Selected instead of Highlighted but that doesn’t work. (The initial image is set in the Attributes Inspector). This is what I have -
#IBAction func buttonTapped(sender: AnyObject) {
ColourTestButton.setImage(UIImage(named: "blank-purple.jpg"), forState:.Highlighted)
}
I realise Highlighted means just that though on another image where I wanted it to change back (in a Cell of a Table View) it stayed, which is why I thought it would work for this one. What do I have to do here to make it stay changed to the new image?
I think you need this code:
#IBAction func buttonTapped(sender: AnyObject) {
let image = UIImage(named: "blank-purple.jpg") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
ColourTestButton.setImage(image, forState: .Normal)
}
UPDATE:
If you want to change the image like play/pause then you can do it this way:
var pressed = false
#IBAction func pressed(sender: AnyObject) {
if !pressed {
let image = UIImage(named: "pauseImage.png") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btn.setImage(image, forState: .Normal)
pressed = true
} else {
let image = UIImage(named: "playImage.png") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btn.setImage(image, forState: .Normal)
pressed = false
}
}
I think you are updating the image only at Highlighted state, so when you release the button it goes back to an original image. Try to change it at Normal state so that it will change the image. Take one flag to check the Play and Pause condition.
#IBAction func buttonTapped(sender: AnyObject) {
if (!flag)
{
//for Play
flag = true
ColourTestButton.setImage(UIImage(named:"USE PLAY IMAGE"), forState:. Normal)
}
else
{
//for Pause
flag = false
ColourTestButton.setImage(UIImage(named:"USE PAUSE IMAGE"), forState:. Normal)
}
}
Though Dharmesh Kheni's answer is OK I would recommend to use two buttons. You can set them in separate function or in interface builder and then just show and hide them according to state. Something like this:
func setButtons()
{
playBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.hidden = true
}
#IBAction func playTapped(sender: AnyObject)
{
playBtn.hidden = true
pauseBtn.hidden = flase
//do other stuff
}
#IBAction func pauseTapped(sender: AnyObject)
{
playBtn.hidden = false
pauseBtn.hidden = true
//do other stuff
}

Resources