exclusiveTouch in subviews added to UITableViewCell - ios

I'm trying to ignore touches for a certain subviews added to my custom UITableViewCell subclass. I set exclusiveTouch to YES for that subview but the touches still trigger the cell selection.
Is there a way to avoid that selection just when touches are in that subiew?
Thanks

In my experience, #Ali3n's solution doesn't work (i.e. using view.userInteractionEnabled = NO). When you tap on the view, the cell is still selected.
However, I've found that if you add a UIButton as a subview, and add your subviews to that then it does work as you'd expect - i.e. you tap on the area covered by the button and the cell is not selected.
If you use a custom style UIButton then it doesn't look any different to a plain UIView anyway.
Here's how I add a UISwitch to the accessoryView with a non-tappable UIButton behind it. If you miss the button and hit the background the cell is not selected.
UIButton* button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 100, 67);
self.accessoryView = button;
self.accessoryView.backgroundColor = [UIColor grayColor];
UISwitch* statusSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 20, 80, 40)];
[self.accessoryView addSubview:statusSwitch];

you can disable user interation on those views using userInteractionEnabled property of UIView
Like :--
view.userInteractionEnabled = NO;

Related

Create an invisible view that is interactable in swift

For prototyping I am looking to create a invisible interaction area.
If I set the alpha to 0, you can't interact withit.
If you set it to
hidden, it also does not receive gesture events.
Why do you want to use a View for this, Instead you could just use a UIButton and set frame to your current view's frame & set its background color to clearColor like below,
self.invisibleButton.backgroundColor = [UIColor clearColor];
This will do the same action & reduce little works like adding Tap gesture and setting some properties for the view if you go with View.
The best solution I have found is to set the background color to an invisible color. You can't see the button but you can interact with it.
In the init I put:
self.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0)
Is there any better way to do this?
Create a UIView enable userInteraction and make clear background color
UIView *viewsample=[[UIView alloc]init];
viewsample.frame= CGRectMake(0, 0, 200, 200);
viewsample.backgroundColor= [UIColor clearColor];
viewsample.userInteractionEnabled=YES;
[self.view addSubview:viewsample];
UITapGestureRecognizer *tapSkip = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(iviewsampleClicked:)];
[viewsample addGestureRecognizer:tapSkip];
method called when it touched
-(void)iviewsampleClicked:(UIGestureRecognizer*)gesture{
}

Having a UIButton be tapped in a UICollectionViewCell

I have a UICollectionView with its cells all laid out.
I have this declared as a subview:
#property (nonatomic, strong) UIButton *aButton;
I then have that declared in each cell like so:
if (_aButton == nil)
{
_aButton = [UIButton buttonWithType:UIButtonTypeSystem];
}
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
The button click method is like so:
- (IBAction) aButtonPressed:(UIButton *) sender
{
// Code never gets heree
}
The if(_aButton== `nil) is needed since cells get reused.
How do I make this work now? Thanks.
Add button action code before button added to view.... May be it will work .
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
I think, the way you are initializing the button is going to give you frame as 0,0,0,0 . So first give it a proper frame and title to be visible on the screen.
Then try tapping on that title and see if it works or not.
It's unclear what you trying to achieve here, but the button is not working because you not setting it's position and size on the cell. It could be done by setting frame or layout constraints.
Try to set button frame:
_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
You can also set background color for the button, just to make it visible on the cell:
_aButton.backgroundColor = [UIColor redColor];
Everything else is correct, button should work.

Interactive Pop Gesture Recognizer blocking button touchupinside event in custom uitableviewcell

I have a UITableViewController that is pushed from a RootViewController. In the UITableView for the UITableViewController, I'm using a custom cell which has a button at the left with frame: CGRectMake(0,0,30,30).
It seems like when I set self.navigationController.interactivePopGestureRecognizer.enabled = NO; in the UITableViewController, the touch events for the button are received, but if I set it to enabled = YES, the touch events on the button are lost:
Here's my code for creating the button inside the custom tableviewcell.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,30,30);
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
Any thoughts/suggestions on how I can fix this would be appreciated!
Thanks.
Set the cancelsTouchesInView property on the gesture recognizer to NO. The default is YES, which means the view to which it's attached won't get to also handle the touches.

inputAccessoryView on UIView

It's easy as pie to add an inputAccessoryView on a UITextField, UITextView, or UISearchBar. However, there's no obvious and easy way to add one for your basic UIView as far as I can tell!
I have a UIView subclass that follows the UIKeyInput protocol. It receives keyboard input to do stuff that isn't related to entering text, so I'd rather not force it to subclass the former specified views because it adds bloat and would expose a bunch of properties that don't do anything, plus I'd need to work around the text entry that occurs natively to those classes (more bloat).
However, my UIView does need an input accessory view on its presented keyboard to function correctly.
Are there any simple ways to go about this? Do I have to register as an observer to the UIKeyboardWillShowNotification in my UIView subclass and add a subview, as an accessory view, to it manually?
Did you try simply adding the inputAccessoryView method to your viewController?
I believe it gets called when the keyboard is shown, so you don't actually have to assign one to each textField or view.
- (UIView *)inputAccessoryView
{
if (!inputAccessoryView)
{
CGRect accessFrame = CGRectMake(0.0, 0.0, 768.0, 77.0);
inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];
inputAccessoryView.backgroundColor = [UIColor blueColor];
UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
compButton.frame = CGRectMake(313.0, 20.0, 158.0, 37.0);
[compButton setTitle: #"Word Completions" forState:UIControlStateNormal];
[compButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[compButton addTarget:self action:#selector(completeCurrentWord:) forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:compButton];
}
return inputAccessoryView;
}

Table footer view buttons

I have some signup form that had email and login text fields as table cells and signup button as button in footer view.
That all functioned superb, here is the code
frame = CGRectMake(boundsX+85, 100, 150, 60);
UIButton *signInButton = [[UIButton alloc] initWithFrame:frame];
[signInButton setImage:[UIImage imageNamed:#"button_signin.png"] forState:UIControlStateNormal];
[signInButton addTarget:self action:#selector(LoginAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.hidesBackButton = TRUE;
self.tableView.tableFooterView.userInteractionEnabled = YES;
self.tableView.tableFooterView = signInButton;
self.view.backgroundColor = [UIColor blackColor];
My question is how to add another button next to this one.
By using [self.tableView.tableFooterView addSubview:...] buttons are not shown and if I use some other UIView, place buttons there and then say that the footerView is that UIView, I see the buttons, but am unable to press them.
I hope that this isn't too confusing and that you understand my problem.
Take a look at this question: UIButtons on tableFooterView not responding to events
your first try is wrong, if you are doing as you as saying you are trying to add a button to the subview of a button:
first you
...
self.tableView.tableFooterView = signInButton;
...
and then later
...
[self.tableView.tableFooterView addSubview:...]
...
but tableFooterView is signInButton. So that is why that is not working.
your second try is correct and the answer yonanderson pointed you should work out and is the correct way to do this, you just need to :
[yourButtonsView addSubView:button1];
[yourButtonsView addSubView:button2];
yourButtonsView.userInteractionEnabled = YES;
self.tableView.tableFooterView = yourButtonsView;
self.tableView.tableFooterView.userInteractionEnabled = YES;
Humm, I didn't read this close enough before answering. I think you need to set the container UIView's userInteractionEnabled property then as you tried, set the footerView to the container with the subviews.
It took me forever to figure out why my footer's buttons were not calling the designated actions. Finally I discovered that I needed to adjust my footer's height. This fixed it for me:
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return footerView.bounds.size.height;
}

Resources