Reputation: 43
I am trying to add a UITextField in a UIScrollView. I cant get the UITextField in the UIScrollView, but clicking on the TextField does nothing. I have tried numerous things. The last thing I tried was making a custom ScrollView and checking the hitTest. I set it to say "NO" on delaysContentTouches regardless and it still does nothing. I set userInteractionEnabled on the TextField and still nothing. I've looked on all the other similar questions and none of them have answers.
The code below show my attempt to add a Label and TextField to a UIView and then to add that UIView in the ScrollView. It all works visually, but the TextField does nothing.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
lbl.text = @"Sample";
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor blackColor]];
UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(115, 0, 195, 30)];
txt.borderStyle = UITextBorderStyleRoundedRect;
txt.font = [UIFont systemFontOfSize:15];
//txt.placeholder = @"enter username";
txt.autocorrectionType = UITextAutocorrectionTypeNo;
txt.keyboardType = UIKeyboardTypeDefault;
txt.returnKeyType = UIReturnKeyNext;
txt.clearButtonMode = UITextFieldViewModeWhileEditing;
[txt canBecomeFirstResponder];
txt.delegate=self;
txt.userInteractionEnabled=TRUE;
[txt addTarget:self action:@selector(tfTouch:) forControlEvents:UIControlEventTouchUpInside];
txt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView *theView = [[UIView alloc] init];
theView.userInteractionEnabled=TRUE;
[theView addSubview:lbl];
[theView addSubview:txt];
[scrollView addSubview:theView];
Upvotes: 1
Views: 2587
Reputation: 9977
Why do you have another view, you could add your textfield & label directly on the scrollview. Probably you just need to set a frame for your view. That should do it:
[theView sizeToFit];
Also set your contentSize correctly. scrollView.contentSize = theView.bounds;
Upvotes: 3