0

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

CGRect applicationFrame = [[UIScreen mainScreen] bounds];
    self.view = [UIView new];
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    self.view.backgroundColor = [UIColor colorWhite];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collection.translatesAutoresizingMaskIntoConstraints = NO;
    self.collection.backgroundColor = [UIColor colorWhite];
    self.collection.contentInset = UIEdgeInsetsMake(0, 7.0, 0, 16.0);

    [self.collection registerClass:[OptionsToCreateCollectionViewCell class] forCellWithReuseIdentifier: [OptionsToCreateCollectionViewCell cellIdentifier]];
    [self.collection setDataSource:self]; // UICollectionViewDataSource
    [self.collection setDelegate:self]; // UICollectionViewDelegate
    
    [self.view addSubview:self.collection];
    
    CGFloat viewWidth = applicationFrame.size.width;
    CGFloat frameWidth = viewWidth;
    if(![MyUIResources isPhone]){
        frameWidth = viewWidth/2;
    }

    [self.view addConstraints:
        @[
            [NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10],
            [NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20],
            [NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:frameWidth],
            [NSLayoutConstraint constraintWithItem:self.collection attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:114*2.5]
        ]
    ];
  • Why are you doing this: `self.contentView.userInteractionEnabled = FALSE;`? – DonMag Mar 08 '22 at 12:26
  • I've seen someone asking similar question on StackOverFlow, that worked for them. So I tried it – Kalyan Revuru Mar 09 '22 at 04:23
  • OK - the problem is with your cell layout. First, if you set `self.contentView.userInteractionEnabled = FALSE;` that ***also*** disables user interaction on `.contentView` subviews -- so your button's `.userInteractionEnabled` is now also false. Second, based on your comment" *"I guess clipsToBounds should be NO, when set to YES, the view got invisible"* -- that means your button is **outside** the bounds of its superview, and therefor cannot be interacted with (tapped). Show your cell layout, subviews, and constraints. – DonMag Mar 09 '22 at 13:23
  • Yeah, sure . I'm adding above to the question. But even though I've set self.contentView.userInteractionEnabled = TRUE; It's not working – Kalyan Revuru Mar 09 '22 at 16:07
  • First, are you doing this in `viewDidLoad`? If so, are you changing the controller's `view` to a new `UIView`? If so, why? Second, you didn't show us anything about your cell layout. – DonMag Mar 09 '22 at 16:18

2 Answers2

0

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

Allen
  • 196
  • 11
0

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.

Akila M
  • 42
  • 7
  • 1. userInteractionEnabled by default for a button is TRUE, so I didn't touched it. But I set FALSE to self.contentView 2. dimensions are correct, verified through layout inspector 3. I did set self.view.clipsToBounds = NO; – Kalyan Revuru Mar 09 '22 at 04:21
  • I guess clipsToBounds should be NO, when set to YES, the view got invisible – Kalyan Revuru Mar 09 '22 at 04:27
  • hi, what reason using contentView userinteraction disabled? and clipsToBounds should be NO, when set to YES: that frame fixed superview example https://stackoverflow.com/questions/20449256/how-does-clipstobounds-work – Akila M Mar 09 '22 at 04:39