1

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?

related: https://stackoverflow.com/a/6825839/4050489

edited to add button code per request:

        self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.myButton.frame = myButtonFrame;
        self.myButton.enabled = interface.isActive;
        self.myButton.userInteractionEnabled = YES;
        self.myButton.exclusiveTouch = YES;

        self.myButton.backgroundColor = [UIColor greenColor];

        //self.myButton. = YES;

        self.myButton.layer.borderWidth=1.0f;
        self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
        self.myButton.layer.cornerRadius = 10;

        [self.myButton setTag:interface.tag];
        [self.myButton setTitle:interface.Name forState:UIControlStateNormal];

        [self.myButton addTarget:self action:@selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];

        [self.myButton setEnabled:YES];  //error check


        [self.partSetupView addSubview:self.myButton];

partSetupView is a UIView in the UIScrollView.

Community
  • 1
  • 1
Bill3
  • 61
  • 9
  • 1
    Please post some code samples as to how you're adding these buttons to the ScrollView – jervine10 Dec 27 '14 at 14:22
  • You can explicitly enable `showsTouchWhenHighlighted` to see if it's even recognizing a touch down event. – jervine10 Dec 27 '14 at 15:06
  • I don't think it does recognize it. You don't even get the text flicker unless added to a `UIView` outside of `UIScrollView`. Plus it doesn't cause the delegate to fire either. `UIScrollView` is interfering or not hooked up right but the solutions I've read so far do not seem to prevent that interference. Does seem to be a common issue. But the text fields worked effortlessly. – Bill3 Dec 27 '14 at 15:17
  • Make sure `userInteractionEnabled` is set to yes for your scroll view – Chris Dec 27 '14 at 15:22
  • Yes, already did that. Also, shouldn't a downvote at least mean that person has an easy answer? – Bill3 Dec 27 '14 at 15:28
  • I don't know why you got the downvote, but I have plenty of buttons inside scrollViews so I'm not sure what could be going wrong. Is your `partSetupView` actually inside the scrollView, not behind it? – jervine10 Dec 27 '14 at 15:44

1 Answers1

2
Try this sample Code:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
    myScroll.backgroundColor = [UIColor redColor];
    [self.view addSubview:myScroll];

    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
    myView.backgroundColor = [UIColor yellowColor];
    [myScroll addSubview:myView];

    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
    myButton.backgroundColor = [UIColor blackColor];
    [myView addSubview:myButton];
    [myButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
    NSLog(@"Button Click");
}
@end
Krunal Darji
  • 568
  • 1
  • 3
  • 11
  • 1
    This helped find the answer. Your button worked. Mine didn't. No real difference between them. Except location. Despite seeing the buttons and despite their scrolling, I didn't have my `UIView` frame (partSetupView frame size) set the same as my `UIScrollView`. I've seen this eluded to but didn't understand what they meant until I fought with it myself. – Bill3 Dec 27 '14 at 18:15
  • Gave you an uptick cause your comment put me on the right path! – PruitIgoe Mar 16 '17 at 14:07