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

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

Related

How to hide segmented control?

Have ViewController with SearchBar.
After click search button appears SearchBar with focus
How to hide 2 segmented controls "Title"?
Alternate to Jeff's answer
If this code segmentedControl.hidden = YES; is not working.. Then Embed the segment control inside UIView and hide that view like this,
view.hidden = YES;
You need to hide the UISegmentedControl, as a UIView subclass it has a hidden property.
segmentedControl.hidden = YES;
I am adding Swift 3 solution so that it will be useful for others. In Swift 3, you can hide UISegmentedControl like this
segmentedControl.isHidden = true

iOS change custom bar button text color when the button becomes available

So I have a registration page (and multiple other places through the registration process where this will be implemented) that has a Next button, which is set up as a custom UIButton dragged onto the nav bar. As users enter in their registration information, I want to change the color of the text from grey when it is unavailable, to a custom green color. I've read some other threads, such as this one , but they haven't been any help. I think I'm doing what they told me to correctly, but I'm getting different results than I want. Right now my code looks like this:
-(void)checkCompletion
{
if( emailEntered && passwordEntered && [_password.text length] >= 5)
{
[_nextBarButtonItem setEnabled:YES];
[_nextButton setEnabled:YES];
[_nextButton.titleLabel setTextColor:[UIColor customGreen]];
[_nextButton setTintColor:[UIColor customGreen]];
[_nextBarButtonItem setTintColor:[UIColor customGreen]];
nextAvailable = YES;
}
else
{
nextAvailable = NO;
}
}
_nextBarButtonItem is the barButtonItem, and _nextButton is the UIButton I dragged onto the navigation bar, which is underneath _nextBarBUttonItem in the hierarchy. customGreen is a category with its header file #include'd into my prefix.pch, as I use the color throughout the app. This function gets called when textfields return and when the password is edited, so I can make the button available before the return key is pressed if users don't want to dismiss the keyboard first.
I have tried several methods, such as making the button my customGreen on the storyboard and disabling both the barButtonItem and the button itself underneath the barButtonItem in the hierarchy, hoping it would grey it out, but the button is still green, you just can't press it. I made the button grey on the storyboard, then call this function, but the text ends up changing to white instead of the green color. I tried explicitly defining the color as I set it, but I get the same result. I do not want my back button to turn this green color, only the next button when it becomes available to press.
Any ideas what I'm doing wrong?

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

How to disable UISlider interaction when view is hidden?

Right now I have a UISlider that I set to hidden when a button is hit, but if I I'm interacting with the UISlider while I hit the button, the slider disappears but I'm still able to move the values of the slider.
I've tried to setUserIneractionEnabled = NO on the button press but that doesn't seem to do the trick.
Any idea on how I should go about disabling this?
EDIT * My code *
- (void)didPressButton:(UIButton *)button
{
if (!self.isShowingDetailView) //
{
self.showingDetailView = YES;
self.valueSlider.hidden = YES;
self.valueSlider.userInteractionEnabled = NO;
}
else
{
self.showingDetailView = NO;
self.valueSlider.hidden = NO;
self.valueSlider.userInteractionEnabled = YES;
}
}
One way to fix is to set the exclusiveTouch property on the button and the slider. This way they can not receive touch simultaneously.
It can be set either in Interface Builder or in code
If the value of the slider is important to you when the button is tapped then you should get and store the value when the button is tapped. Equally, you should set the slider value just before it is shown again.

Dismiss Keyboard

So I have a self created top bar controller that is being implemented in my other controllers views. I have a textfield on this top bar. I was wondering what the best approach to having the keyboard dismiss if the user clicks anywere outside the keyboard. I do have a tap gesture recognizer that performs the method dismisskeyboard. However, this only works if the user clicks on the top bar outside the keyboard. Is there a way to set it up so if the user clicks anywere on the screen, then this will dismiss the keyboard?
The approach I would describe is a hack but still works.
create a transparent UIButton with the frame of the view, like below:
UIButton* overlay = [UIButton buttonWithType:UIButtonTypeCustom];
overlay.frame = self.view.bounds;
overlay.backgroundColor = [UIColor clearColor];
[overlay addTarget:self action:#selector(hideOverlay:) forControlEvents:UIControlEventTouchDown];
[self.view.subviews[0] insertSubview:overlay belowSubview:self.textField];
Create a method hideOverlay to dismiss the keyboard and hide the transparent:
-(void)hideOverlay:(id)sender {
UIView* overlay = sender;
[overlay removeFromSuperview];
[self.textField resignFirstResponder];
}
You should ideally call the first block of code in textFieldDidBeginEditing: protocol method of UITextFieldDelegate and you should register your calling class accordingly.
You might try giving the text field a transparent inputAccessoryView, sized to fill the rest of the screen, that catches taps and dismisses the keyboard.

Resources