UIButton doesn't work in UIScrollview - ios

I have UIScrollview and view. In view I have UIButton which doesn't work, if I have UIButton in UIScrollview works. I have User Interaction Enabled on both.
for button I have this method:
- (IBAction)search
{
NSLog(#"SEARCH");
}
Whatever I give to the view doesn't work.
I solve the problem. View was not properly configured (Constraints in autolayout)

Try this
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return ![view isKindOfClass:[UIButton class]];
}
hope it helps.

When an UIButton doesn't send the message to the IBActionreceiver is because the button view is not contained in the superview.frame
Try it setting a backgroundColor to the superview
Take a look there: UIButton selector doesn't work in nested UIViews

Related

UIButton not working in UITableview

Have UITableviewcell --> inside which i have 4 different UIView and 3 of the views has UIButtons. I want to make UIButton clickable. For the first time the buttons are clickable but when i go to next screen and come back, the buttons don't work. Please let me know how to implement this? Thanks!
PHMyTripView *tripsView=[[PHMyTripView alloc] initWithFrame:CGRectMake(10, 5, self.frame.size.width-20, cell.frame.size.height)];
tripsView.onBaggageClick = ^{
[weakSelf handleBaggagePurchaseTapped];
};
if ([data count]>0) {
[tripsView fillTripData:[data firstObject]];
[tripsView showAncillaries:self.predictiveManager.upcomingCDAirBookingInfo];
}
[cell bringSubviewToFront:tripsView.bagsButton];
[cell.viewPlaceHolder addSubview:tripsView];
This happens because the cells are reusable and if you go to another screen the button may kinda mess round into different cell in UI or functionally.
The way to solve this is to add tag. You can define cell.tag = indexPath.row * 10 or so.
Then when draw the button, you check the condition, if cell.tag == ?, then add your button.
Thank you everyone for your response but they din't work in my case.
Here is the answer. Since the UITableview was reloading when it comes back to the screen.The frames were mis placed and Hence the button was not able to click.
Used this one line code which worked fine.
self.tripsView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
You should turn off delaysContentTouches on each UITableView subview, which is an instance of UIScrollView class.
Subclass UITableView and override initWithFrame method:
.h file
#interface NoDelaysOnTouchTableView : UITableView
#end
And .m file
#import "NoDelaysOnTouchTableView.h"
#implementation NoDelaysOnTouchTableView
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame: frame];
if (self) {
for (id view in self.subviews) {
if ([NSStringFromClass([view class]) isEqualToString: #"UITableViewWrapperView"]) {
if ([view isKindOfClass: [UIScrollView class]]) {
// turn OFF delaysContentTouches in the hidden subview
UIScrollView* scroll = (UIScrollView*)view;
scroll.delaysContentTouches = NO;
}
break;
}
}
}
return self;
}
#end
Next - use this subclass (create an instance of NoDelaysOnTouchTableView) to be able to press UIButton immediately.
I think that the causes would be multiples.
You can try:
check the constraints for each button in your cell
in cellForRow you can do addTarget: for each button with relative #selector and in this method check the object (datasource) associated to button.
do not use addSubView in your cell, instead use a Xib (or define your cell in a storyboard within tableView) so you can set the constraints.
I Hope, i've helped you.
Not specifically about this issue, but for some people it may be the case.Try to add subviews in contentView of the cell.
Instead of addSubview(textField) use contentView.addSubview(textField)

UIButton action not get worked in UIView

I have a view of UIView subclass and 2 buttons and UITableview inside the view. In design part, one button first, then UITableView and then second button. When I click the first button(that is the button above the tableview), It gets the button action. But when I click the second button that is the button below UITableView it do not get the button action. If i drag the second button to above the position of tableview it get the action. Please help me.!
Well, when you have a UIView with userInteractionEnabled = YES, it gets all the touches, therefore, any UIView behind this view wont get touched.
You can use a solution with - hitTest:withEvent::
You need to subClass your UITableView, pass it a pointer to you 'behind' UIButton, and override - hitTest:withEvent: in your CustomTableView, like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isInsideButton = CGRectContainsPoint(self.behindButton.frame, point);
return isInsideButton ? nil : [super hitTest:point withEvent:event];
}
Hope it will help.

UISlider in UITableView not responding to swipe gesture

I try to insert UISlider to UITableViewCell, but swipe gesture doesn't work correctly. For sliding needed hold and move the thumb, but i want to get swipe gesture without holding. I think tableview's own gestures not allow do this, but i don't know how to disable it.
Use - gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
to set the property to YES. Then you can add a check in a function to decide which gesture to act on.
I've encountered the same problem recently. It happens for me in static cell of UITableViewController, it's instantiated from storyboard. I have found an ugly workaround, but will be happy to see a better solution for this.
So i've disabled all gesture recognisers of self.view and self.view.superview of UITableViewController:
- (void)disableGestureRecognisersInView:(UIView*)view {
for ( UIView *subview in view.subviews ) {
for ( UIGestureRecognizer *rec in subview.gestureRecognizers ) {
rec.enabled = NO;
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self disableGestureRecognisersInView:self.view];
[self disableGestureRecognisersInView:self.view.superview];
}
And now UISlider works normally, didn't noticed any other problems because of this workaround too. But i still don't like it.

UITableView didSelectRowAtIndexPath Not Selected on UIPageViewController

I have a UIViewController that has UIPageViewController's view added to it. That UIPageViewController has a page with a subview that has a UITableView added as a subview. I am using iOS 6 and the method didSelectRowAtIndexPath is not being called when I click on the cell. There is a weird "bug" though... If I turn the page halfway and then come back to the page I was on (without completing the page turn), I'm then able to select the cell. I assume this has something to do with gesture recognizers, but I can't figure it out. I tried removing the gesture recognizers from the instantiation of the UIPageViewController, but was unable to get that to work.
I remove the tap gesture recognizers from the UIPageViewController like so, but still the buggy behavior exists...
for (UIGestureRecognizer *recognizer in pageViewController.gestureRecognizers) {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
recognizer.enabled = NO;
}
}
Any suggestions?
Subclass UIView with UITableView. let say myUIView.
Implement the UITableView delegates in myUIView.
Add myUIView as a subview of UIPageControl.
UITableView delegates in myUIView will trigger.
It turned out to be something very simple. On my UIViewController I had the autoresizingMask set to FlexibleHeight. Changing it to None for some reason fixed my problem.
// self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Changed to :
self.contentView.autoresizingMask = UIViewAutoresizingNone;
If anyone can shed some light onto the reasons why this is the case, I'd appreciate it.

Detect when my UIView subclass is added to another UIView

I want my UIView subclass to position itself automatically when it is added to a parent container view.
Can I somehow detect when it is added and run my positioning code then or do I need to do something like?
[parentView addSubview:subView];
[setView calcPosition];
UIView provides the methods willMoveToSuperview: and didMoveToSuperview. Just override those to know when the view is added to another view (or later removed).
Write calcPosition methord inside the subview and call it from the didMoveToSuperview of subview
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
[self calcPosition];
}

Resources