Home > OS >  Can't able to click a UICollectionViewCell in a UICollectionView
Can't able to click a UICollectionViewCell in a UICollectionView

Time:03-08

Let's say I have a view controller OptionsToCreateViewController which inherits UICollectionViewController and I have used that View Controller in another view controller like this.. I can able to see the view but can't able to click the cell

self.optionsToCreateViewController = [[OptionsToCreateViewController alloc] init];
        self.optionsToCreateViewController.view.translatesAutoresizingMaskIntoConstraints = FALSE;
        [self.optionsToCreateViewController didMoveToParentViewController:self];
        [self addChildViewController:self.optionsToCreateViewController];
        [self.view addSubview:self.optionsToCreateViewController.view];
    
        OrganizerBottomBar *orgBB = [OrganizerBottomBar new];
        CGRect applicationFrame = [[UIScreen mainScreen] bounds];
        CGFloat viewWidth = applicationFrame.size.width / 2;
        if(![MyUIResources isPhone]){
            viewWidth = applicationFrame.size.width / 4;
        }
        [self.view addConstraints:@[
            [NSLayoutConstraint constraintWithItem:self.optionsToCreateViewController.view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:-(orgBB.frame.size.height   75)],
            [NSLayoutConstraint constraintWithItem:self.optionsToCreateViewController.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:-viewWidth]
        ]];

is this the right implementation of a view controller in another view controller

In Collection view cell:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
    self.contentView.userInteractionEnabled = FALSE;
    self.userInteractionEnabled = TRUE;

I also put a break point to buttonPressed function, it's not hitting. i also used

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [self->button hitTest:[self->button convertPoint:point fromView:self] withEvent:event];
    if (view == nil) {
        view = [super hitTest:point withEvent:event];
    }
    return view;
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if ([super pointInside:point withEvent:event]) {
        return YES;
    }
    return !self->button.hidden && [self->button pointInside:[self->button convertPoint:point fromView:self] withEvent:event];
}

but no use. Could anyone please help me here, I'm stuck here badly

CodePudding user response:

What is the button's superview? Add the button on the cell.contentView, not cell self.

CodePudding user response:

1.check that button frame(if outside cell not working, then any view overlapping that button not working ) and user interaction enabled or not

Otherwise 2.check that cell frame and height(check if there inside of collection view)

Otherwise 3.collection view supper frame set correctly (collection view frame need inside of superview) If Frame working correct so assign that clipsToBounds = YES self.yourview.clipsToBounds = YES;

notes:past that button Action in collection didselecteditem . then check working or not because easily identified issue only that button or that class based.

  • Related