How do I disable a UIButton? - ios

I am working on a project in which I have to show all photos of Photo Library in a plist and show them horizontally on UIButtons.
My app will also have an edit button: when the user clicks this button, a delete mark (such as usually appears in other iPhone/iPad apps) should show on each button.
But here's the crucial bit: as soon as this delete mark appears, the functionality of the button should be disabled. I've tried accomplishing this with the following:
{
editbutton.enabled=NO;
}
...but it neither produces an error nor works. What should I do?

Please set this...
editButton.userInteractionEnabled = NO;
or You can use
editButton.enabled = NO;

Swift 3 and above
editButton.isEnabled = false
setEnabled is now part of the setter method for isEnabled.

setter for property enabled is overridden in class UIButton. try to send a message.
[editbutton setEnabled:NO];

Use the enabled property of UIControl which the super class of UIButton and set it with NO.
myButton.enabled = NO;
You could also try as #Marvin suggested,
In that case your button will not respond to any touch event from user,

In addition to the above:
Set
editButton.userInteractionEnabled = NO;
or you can use
editButton.enabled = NO;
You might want to gray out the button so the user knows it was disabled:
editButton.alpha = 0.66

yourBtn.userInteractionEnabled = NO;

Related

UITapGestureRecognizer Not Getting Called

I am adding the following UITapGestureRecognizer to my view.
- (void)viewDidLoad {
_focusView = [[PBJFocusView alloc] initWithFrame:CGRectZero];
_focusTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(_handleFocusTapGesterRecognizer:)];
_focusTapGestureRecognizer.delegate = self;
_focusTapGestureRecognizer.numberOfTapsRequired = 1;
_focusTapGestureRecognizer.enabled = NO;
[_previewView addGestureRecognizer:_focusTapGestureRecognizer];
}
- (void)_handleFocusTapGesterRecognizer:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"Focus");
}
_focusTapGestureRecognizer should be enable. Try this
_focusTapGestureRecognizer.enabled = YES;
OR
Remove this line
_focusTapGestureRecognizer.enabled = NO;
Try this inside your code
_focusView.userInteractionEnabled = YES;
May be helpfull...
_focusTapGestureRecognizer.enabled = NO;
Remove this line
To make this work check these three things as it is suggested by other guys i only writing them step by step
Check did you set the delegate of TapGesture in .h file.
Check on the UIView you are adding gesture its userInteractionEnabled is True.
Make that enable of UITapGesture Yes .//Why it is no ?
Fourth is , did you gave any size to the UIView , for checking that add some background color to it,see is it visible or not .
if these 4 doesnot help you out type your code what you used it .
You might also want to check that you're not trying to reuse the UITapGestureRecognizer object for different views.
It seems like you can because you add the gesture to the view, but in ultimately the view needs to be assigned to the view property on the gesture object so adding the gesture object to a second view will just overwrite the first.

UITextField won't becomeFirstResponder

I'm having a problem getting a textfield to accept the becomeFirstResponder directive.
I'm providing a custom mechanism to create a title in the navigation bar. I have another viewcontroller that is successfully using this same technique. On viewDidAppear I fire off:
- (void)addTitleTextField
{
CGRect textFrame = self.parentViewController.navigationController.navigationBar.frame;
textFrame.size.width = 300.0;
textFrame.origin.y = (768.0 - 300.0)/2;
textFrame.size.height = 30.0;
textFrame.origin.x = 7.0;
self.titleTextField = [[UITextField alloc] initWithFrame:textFrame];
self.titleTextField.placeholder = NSLocalizedString(#"New Multiple Choice Quiz", #"New Multiple Choice Quiz");
self.titleTextField.borderStyle = UITextBorderStyleRoundedRect;
self.titleTextField.font = [UIFont boldSystemFontOfSize:20.0];
self.titleTextField.textAlignment = NSTextAlignmentCenter;
self.titleTextField.backgroundColor = [UIColor whiteColor];
self.titleTextField.textColor = [UIColor blackColor];
self.titleTextField.delegate = self;
[self.titleTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
self.titleTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.titleTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
self.activeTextField = self.titleTextField;
self.parentViewController.navigationItem.titleView = self.titleTextField;
[self.titleTextField becomeFirstResponder];
}
self.titleTextField will allow me to set the text value, but if you check using canBecomeFirstResponder it returns NO. As you can see I am setting this on the parentViewController. I've tried using a delegate to attempt to get the parentViewController to set it. When I do the delegate and check whether the textField canBecomeFirstResponder it returns YES, but I'm still unable to make it accept the firstResponder order. Any ideas? The Docs say "A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder".
Are you telling the UITextField to become selector on a background thread?
[textField performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil
waitUntilDone:YES];
Rationale: Calling UIKit methods (ie updating the view) on a method other than the main thread won't work. . this could be happening. (It is not clear where the addTitleTextField method is being called from).
Is there another first responder that needs some time to resign?
[textField performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
Rationale: If another field is hanging onto first responder (or in the process of resigning it), it will give it time to clean up an resign, by waiting until the next run-loop. . . . . usually the next run-loop will be enough time for the previous responder to clean up, or you could try a short delay like 0.05.
You might need to add textField to view hierarchy:
v.addSubview(textField)
Another thing to do is to make sure the textField is enabled (check Interface Builder, then any disabling you might do through code).
I spent almost an hour trying to figure out what the problem was: I got a workaround by subclassing UITextField and overriding canBecomeFirstResponder to always return true. This forced it to finally show keyboard and edit the textField, but I noticed I couldn't tap/select/copy/paste the textField. User Interaction was enabled, and then I finally saw the Enabled not being checked...I blame Apple for not displaying any visual cue when a piece of UI is disabled (like the do with Hidden) lol

Handling button actions

I have 3 buttons named as : bw1 , bw2 and bw3
Simply, I want a specific action to be applied when one of these buttons is clicked using hidden.
Each button should only hide itself when clicked.
My problem is when bw3 is clicked, both bw3 and bw2 are hidden. But when I click bw2, nothing wrong happened which means that the problem is in bw3 method.
I don't know what the problem is. I traced the code many times but I could not find the solution.
- (IBAction)bw1:(id)sender {
_lw1.hidden = NO;
_bw1.hidden = YES;
}
- (IBAction)bw2:(id)sender {
_lw2.hidden = NO;
_bw2.hidden = YES;
}
- (IBAction)bw3:(id)sender {
_lw3.hidden = NO;
_bw3.hidden = YES;
}
As per my comment above
check your xib, for _bw3, there are 2 IBAction set, bw2 and bw3.
i.e. - (IBAction)bw2:(id)sender and - (IBAction)bw3:(id)sender
Hope it helps you..

Disabling zoom-in zoom-out functionality of the UIWebView

I am new to iOS development and I am developing an application which will show and web page in UIWebView. I want to get rid of the default zoom-in and zoom-out functionality of the webview.
This is in the Apple Documentation:
#property(nonatomic) BOOL scalesPageToFit
Discussion
If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.
Interface Builder Approach:
Make sure that the "Scale pages to fit" tick box in the right-side column of the interface builder when you click on your webview is un-ticked. It's right at the top.
You're also going to need to un-tick the box that says "Multiple Touch" around half way down. That will disable pinching to zoom.
Code Approach:
You can also do it programmatically with this code:
webView.scalesPageToFit = NO;
You're also going to want to add:
webView.multipleTouchEnabled = NO;
That will disable the ability to pinch and zoom, since it disables the ability to use multiple fingers in an action, and pinching requires two, obviously!
set the UIWebView property scalesPageToFit to NO, will disable the user zooming
myWebView.scalesPageToFit = NO;
One simply needs to do:
myWebView.scalesPageToFit = NO;
and also disable the pesky pinch to zoom:
webView.multipleTouchEnabled = NO;
For me the ideal solution was, surprinsingly, in the html side only. Just add this in <head>:
<meta name="viewport" content="user-scalable=0" />
I don't recommend to use webView.scalesPageToFit = false because it brings issues like making everything 4x bigger. I also did not use initial-scale=1.0, maximum-scale=1.0 nor minmum-scale=1.0
UIScrollView *scrollView = [webView.subviews objectAtIndex:0];
scrollView.delegate = self;//self must be UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
try this ... It works..
Disable bouncesZoom of the scrollView will solve your problem. Give it a try!
webView.scrollView.bouncesZoom = false
When inheriting from UIWebView you can implement. (Works from iOS 5, lower not sure)
- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
return nil;
}
Try this:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
Swift Solution
Simply set the value mentioned above to false: webView.scalesPageToFit = false
To add to the answers, this will work:
webView.scrollView.multipleTouchEnabled = NO;
i am also struggle at first when i use webview scroll event after that i fond the below code for disable the scroll in webview and this also remove the multitouch gestures function in that particular webview.
[[[theWebView subviews] lastObject] setScrollEnabled:NO];
here theWebView is the name of my UIWebView..
use this code in webViewDidFinishLoad function ,,, its really helpful to me,..

Change background image of a button without extra buttons

I've set a button with an image as it's background, and I'm trying to get the image to change back and forth from 2 different images when pressed.
For example, a happy face when pressed would switch to a sad face, and when pressed again would switch back to a happy face and so on.
Any help you can offer would be amazing before I pull anymore hair out.
set the images for two different states: normal and selected,
have the button target an action that toggles the selected state on the button/sender
UIButton* myButton;
- (IBAction)buttonPressed {
if(alreadySelected) {
myButton.imageView.image = myNormalImage;
} else {
myButton.imageView.image = mySelectedImage;
}
}
Not sure, but I think this should do it.
What BoopMeister said, except as bshirley pointed out, you need this API call:
[myButton setImage:happyImage forState:UIControlStateNormal];

Resources