How to disable UISlider interaction when view is hidden? - ios

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.

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

Clear button (x) of textfield not working if auto keyboard hiding is used at the same time

I'm using a gesture recognizer like this solution to hide the keyboard if the user taps outside of one of my textfields. In my case I'm using a scroll view with some textfields on top. There is also some auto-scroll feature implemented (that's why I'm using the scroll view).
I can hide the keyboard if the user taps outside of it. This is working. I have also enabled the clear button (x button on the right) of the textfield. If the user taps on it the keyboard is hidden, but the content of the textfield is not cleared. Normally I would expect that the content is cleared and the keyboard is not dismissed in this case. This problem was also found by Patrick.
I tried to get the tapped object of the UITapGestureRecognizer, but this seems to be the UIScrollView. How can I get the clear button and the auto keyboard hiding feature working?
A generic solution would be nice that would work for all textfields. To complete my question I add my code (which is in C#):
UITapGestureRecognizer tapGesture = new UITapGestureRecognizer ();
tapGesture.CancelsTouchesInView = false;
tapGesture.AddTarget (() => HandleSingleTap (tapGesture));
this.scrollView.AddGestureRecognizer (tapGesture);
private void HandleSingleTap(UITapGestureRecognizer recognizer){
this.scrollView.EndEditing(true);
}
You can of course provide your solutions for Objective-C.
you should be able to convert the tap location from the gestureRecognizer so you can see if the textField was tapped
I didn't verify it but something like this should work:
- (void)handleTap:(UITapGestureRecognizer *)sender {
BOOL tappedTextField = NO;
UIScrollView *scrollView = (UIScrollView *)sender.view;
for (UITextField *textField in self.textFields) {
CGRect textFieldBounds = textField.bounds;
CGRect textFieldFrameInScrollView = [textField convertRect:textFieldBounds toView:scrollView];
CGPoint tapLocationInScrollView = [sender locationInView:scrollView];
if (CGRectContainsPoint(textFieldFrameInScrollView, tapLocationInScrollView)) {
tappedTextField = YES;
break;
}
// Might work as well instead of the above code:
CGPoint tapLocationInTextField = [sender locationInView:textField];
if (CGRectContainsPoint(textField.bounds, tapLocationInTextField)) {
tappedTextField = YES;
break;
}
}
if (!tappedTextField) {
// handle tap
}
}

Disable UIButton from interaction without changing UIButton look (i.e. greyed out look)

I have the following code where my goal is to disable a UIButton from interaction once it is in the selected mode.
if (...){
cell.requestButton.selected = YES;
cell.requestButton.enabled = NO;
} else {
cell.requestButton.selected = NO;
cell.requestButton.enabled = YES;
}
The side-effect of disabling the UIButton is that it also changes the Button's appearance. Even if I set the same UIImage for both the button's both selected and disabled state, the appearance still takes on a greyed-out looked with a <1 alpha level. Is there a way that I could prevent the disabled state from changing the appearance of the UIButton at all?
Thanks!
Try setting the UIView's userInteractionEnabled property instead. Not sure if that works, but give it a try.
If that doesn't work, you could place a dummy UIView right over it, add constraints to use the original button's position and size, and have it enabled so that it swallows any taps.
Either set the same style as the UIControlStateNormal to the UIControlStateDisabled or have a look at adjustsImageWhenDisabled.

make button toggle method — objective-c

I have 4 buttons. When they are taped they each create a UIView underneath it, they expand under the buttons. I want the view to go back to its original when the buttons are taped a second time. How is this done?
Set an instance BOOL and flip it accordingly. In the IBAction the button is tied to check the BOOL and call the appropriate method to adjust the view.
You can use the selected property of your button, something like:
-(void)yourButtonIsTapped:(UIButton*)button {
if(button.selected) { //first time
//expand the view
button.selected = NO;
}
else { // second time
//hide view
button.selected = YES;
}
}
You can link the buttons from the IB th this method for the touchUpInside event but you will have to change the return type from void to IBAction.
And I think there are several other solutions for this case but this is the faster one and the easiest to explain.
Set every tag button to 0, change it to the opposite value when you press the button. Here is what you'll get:
First time that you press the button: button's tag = 0, expand the view and turn it to 1.
Second time you press the button: button's tag = 1, collapse it and turn it to 0.
So you haven't to create a bool for every button but you can use your button's tag variable.
Code should be like this.
- (void)handleButton:(UIButton *)button{
if(button.tag == 0) { <expandButton> }
else { <collapseButton> }
button.tag = !button.tag;
}
You can remove a view simply by calling
[viewUnderButton removeFromSuperview];

Dynamically updating buttons on a view in IOS?

I hope I can ask this question clearly, I have a viewController that I build programmatically (not with NIB), this view controller has two buttons I draw on the lower portion of the view
"prev" and "next" what I'd like to do is, when I've reached the "end" I'd like to only draw the "prev" button, and not the "next", and vice-versa, when I'm at the beginning, I'd like to only draw the "next" and not the prev.
Any general ideas on how to approach this ?
Thanks,
uba
You can use "hidden" property of UIButton:
if (page == firstPage) {
self.myButtonPrev.hidden = YES;
} else {
self.myButtonPrev.hidden = NO;
}
if (page == lastPage) {
self.myButtonNext.hidden = YES;
} else {
self.myButtonNext.hidden = NO;
}
The first thing to do is to addTarget for your button to listen to touch events
[yourButtonNameHere addTarget:self action:#selector(yourCallBackFunctionNameHere:) forControlEvents:UIControlEventTouchUpInside];
What this does is whenever your button is pressed it calls the yourCallBackFunctionNameHere function. Do this for the other button too.
The semi colon after the function indicates that the function has to send the information of the UIElement that caused the event to occur.
Assign tags to both the buttons.
youButtonNameHere.tag=yourTag;
In the function check which button is sending the UIcontrolEvent by comparing the tags
- (void)yourFunctionNameHere:(id)sender {
UIButton *yourButton =(UIButton *)sender;
if(yourButton.tag==501){
// logic to check if this is the first or last page and act accordingly
yourButton.hidden = YES / NO based on what you want to do.
}else{
// logic to do otherwise.
}

Resources