Hide activity indicator - ios

On my main storyboard I created an Activity Indicator.
I want to hide my activity indicator until the button has been pressed.
Is there a way I can do that?
When I press the button the activity indicator starts animating.
self.indicator.hidden = NO;
[self.indicator startAnimating];
[self performSelector:#selector(showData) withObject:nil afterDelay:2.0f];
Can I hide the activity indicator until the button has been pressed, and then show this activity indicator?

Select the Activity Indicator in Storyboard, then select property "Hides when stopped". This way you don't have to hide it, just start or stop the animation, and the Activity Indicator will show and hide automatically. Of course, you still have to add the code to start and stop the animation to buttons.

Swift 3 Xcode 8.3.2
First hide your activityIndicator in viewDidLoad() method and set hidesWhenStopped property to true.
override func viewDidLoad(){
super.viewDidLoad()
self.activityIndicator.isHidden = true
self.activityIndicator.hidesWhenStopped = true
}
Later when you want to show activityIndicator :
self.activityIndicator.isHidden = false
self.activityIndicator.startAnimating()
And when you want to stop it use :
self.activityIndicator.stopAnimating()

Yes, you can select Hidden property in the Storyboard, and change it in your button action method when you tap it. But you can just select Hides when Stopped and your activity will be hidden if not animating and show up otherwise.

You can add
self.indicator.hidden = YES;
to your UIViewController's viewDidLoad method or select Hidden checkmark in Storyboard.

if you are using swift then you can do it when you set the outlet of your indicator, like-
#IBOutlet weak var indicator:UIActivityIndicatorView!{
didSet{
indicator.hidesWhenStopped = true
}
}
Basically what it meant is, set my activity indicator outlet's hidesWhenStopped property to true when it is established.

Related

IOS - how to prevent button in background from being tapped?

I have a see through UIView in front of a UIButton. There are some other UIViews on the transparent UIView that the user can interact with.
How do I prevent the user from being able to click the button in the background?
I have tried to assign first responder status to the transparent UIView, but that doesn't work and doesn't make any sense because how would the user interact with the other visible UIViews on top?
just set [self.myButton setEnabled:NO]; when your transparent view is open. Enable the button when you want to allow click
self.navigationItem.rightBarButtonItem.enabled = NO;
swift version
button.isEnabled = false
for navigation bar button
self.navigationItem.rightBarButtonItem?.enabled = false
Objective -C
self.buttonName.isEnabled = YES; //button is Enabled
self.buttonName.isEnabled = NO; // button is Disabled

How to disable fast touches and double opening view?

Please help me with problem -
I have button, or label, or textfield at UiViewController view, which have method to open another view, if i touch it.
But if i touch button, label or textfield very fast, it can open two, or three, or more same views.
How to disable this opening? How to open only one view?
How i can do this for all project?
Thanks!
when you click on button then Calling method of the Button, Inside the button method you disable your button, and after when you dismiss open view then again you enable your button.
-(void)YourButtonMethod
{
YourBtn.enable = false;
}
// Enable your YourBtn when dismiss yourView.
It is usually easier to disable the UIButton when you enter the function and re-enable it before leaving. Would look something like this function
-(void)buttonTapped:(UIButton *)aButton
{
aButton.enabled = false;
// do your work here
// and before leaving
aButton.enabled = true;
}
But in your case you are pushing a UIViewController, so you should move the line aButton.enabled = true to your prepareForSegue function.

UIActivityIndicatorView weird behavior when stopping and starting

I have a UIViewController and in its respective storyboard scene I have an activity indicator connect to the controller like this:
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
I want to hide this activity indicator when the view is first launched, then after the users presses a button, inside its #IBAction, I want to start animating the indicator. So I wrote this:
override func viewDidLoad() {
super.viewDidLoad()
activityIndicator.stopAnimating()
}
and in my #IBAction I have:
#IBAction func attemptLogin(sender: UIButton) {
// Start the activity indicator
activityIndicator.startAnimating()
}
But this code does not work. The screen is launched without showing the activity indicator but when the button is pressed, it does not start animating and it does not even appear.
I have tried setting the .hidden property of this object but didn't get anywhere. I do not really want to add this indicator programmatically. Is there a way to make it work?
Try out dynamically create UIActivityIndicatorView
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
[spinner startAnimating];
[spinner release];
There's a couple of things you could consider here as the root of your problems:
The activity indicator is not attached to the IBOutlet
The button's target is not attached to the IBAction (put a breakpoint in your method to be sure)
The default state of an activity indicator is not animated, so your line: activityIndicator.stopAnimating() is redundant.
Depending on your view's layouts/constraints, the UIActivityIndicator might be outside of your screens frame. Try printing out the activity indicators frame with NSLog(#"%#", NSStringFromCGRect(activityIndicator.frame)) before you start the animation.
Hope this helps!

how to disable a navigation bar button item in iOS

I have created a navigation controller. In the second view (which is pushed), I have some webservice call and placing a overlay view and setting
self.view.userInteractionEnabled = NO ;
Once web service call is complete, then I am reverting to
self.view.userInteractionEnabled = YES ;
When I do this, every other buttons except the buttons on the navigation bar are disabled. How to disable those two navigation bar button items ? (a button similar to back button, which pops to first view controller and another button which gives info about help).
I have tried using self.navigationItem.backBarButtonItem.enabled = NO. But still I am able to tap on the button and can navigate to first screen. How can I disable these two buttons ?
Try this
Recommended
self.navigationItem.leftBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem.enabled = NO;
Or Simply Disable by
on Edge case
self.view.window.userInteractionEnabled = NO;
Update:
Recently Apple doesn't allow the back button to enable / disable. Instead of that we can hide it.
self.navigationItem.hidesBackButton = YES;
You can do the following if you are running on Swift
self.navigationItem.rightBarButtonItem?.enabled = true
This snippet will disable the button.
Just disable your UINavigationController view and navigation bar interaction:
self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.view.userInteractionEnabled = NO;
And enable it when you need it back:
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.view.userInteractionEnabled = YES;
Latest Swift: To hide the back button, you MUST use:
self.navigationItem.setHidesBackButton(true, animated: false)
Note: This can trigger a bug in the navigation bar that can cause an artifact to appear in place of a hidden back button when transitioning to a view that doesn't have a back button (or has a leftButton in its place). The artifact that appears is either ellipses "..." or the title of the previous viewController on the stack. I believe this bug to be related to the bug documented in apple's own sample code project "CustomizingUINavigationBar", CustomBackButtonViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}
This code should work on Swift 4.2
self.navigationItem.rightBarButtonItem?.isEnabled = false
The above code will disable the button. To enable it switch the boolean to true
Updated for Swift 3:
If you want to disable a navigation bar button item OR you want to disable hole UINavigationBar i.e. all item present on navigation bar, use below lines of code;
// if you want disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false
// if you want enable again
self.navigationController?.navigationBar.isUserInteractionEnabled = true
Enjoy...!
For version iOS 10.3, swift 3:
self.navigationItem.rightBarButtonItem?.isEnabled = false.
Try this code:
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
This will stop user to interaction with app and after service call, write this code again:
UIApplication.sharedApplication().endIgnoringInteractionEvents()
Sure this will help.
The simplest way to truly disable a UIBarButtonItem would be as followed:
barButtonVar.isEnabled = false
I solved this by just adding a property to my viewcontroller:
#property (nonatomic, strong) IBOutlet UIBarButtonItem * RightButton;
I then connected it to the button on the storyboard.
You can then at will set its properties like:
self.RightButton.enabled=true;
One line solution
self.navigationItem.leftBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem.enabled = NO;
Swift 5 & iOS 13 :
To remove all left buttons or just a specified one just remove from leftBarButtonItems Array.
self.navigationItem.leftBarButtonItems = []
Navigation bar button items must be toggled by referring to them via the navigationItem property.
For example:
func setupNav() {
let saveButton = UIBarButtonItem.init(barButtonSystemItem: .save, target: self, action: #selector(onSavePressed))
navigationItem.rightBarButtonItem = saveButton
saveButton.isEnabled = false
}
func validateSave() {
saveButton.isEnabled = isConditionMet // WON'T work
navigationItem.rightBarButtonItem.isEnabled = isConditionMet // WORKS!
}
Swift 5
self.navigationItem.rightBarButtonItem?.isEnabled = true;
self.navigationItem.rightBarButtonItem?.isEnabled = false;
var menuBtn = new UIButton(UIButtonType.Custom);
menuBtn.Frame = new CGRect(x: 0.0, y: 0.0, width: 20, height: 20);
menuBtn.SetImage(new UIImage("filter"), UIControlState.Normal);
menuBtn.Alpha = 0.05f; //to set the Alpha
menuBtn.Enabled = false;
tested on Mvvmcross Xamarin.iOS only
Swift 5
It's working for Navigation controller
//Disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false
//Enable
self.navigationController?.navigationBar.isUserInteractionEnabled = true

UISearchBar - How can I prevent dismissing the scopeBar when the cancel button is tapped?

I want my UISearchBar's scopeButtons to remain visible at all times, but even though I've set searchBar.showsScopeBar = YES', the buttons are still dismissed with an animation if I begin a search and then cancel it.
Is there any way I can prevent the scopeBar being animated out when the search is cancelled?
Since the scope bar is only meant to be used while the UISearchController is active, I approached the problem differently.
I make the UISearchController active immediately.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Immediately activating the searchController keeps scope bar permanently visible
searchController.isActive = true
// Tapping the cancel button triggers this method, which quickly toggles the scope bar
// But the cancel button isn't needed anyway, so hiding it solves the problem
searchController.searchBar.showsCancelButton = false
}
But now that the UISearchController is always active, by default the UINavigationBar is hidden, which is probably not what you want. To fix that, I prevent it from being hidden in viewDidLoad
searchController.hidesNavigationBarDuringPresentation = false
This approach means that I don't need to use showsScopeBar at all, because it is always visible while the UISearchController is active.
In case anyone is interested, the following delegate method does it.
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsScopeBar:YES];
return YES;
}

Resources