I have an UITextField in a UIScrollView. But when I tap on the textfield so that I can focus on it and type, the UIScrollView registers this as a tap/scroll. As a result, I cannot focus on my textfield and cannot type anything.
<code>self.scrollView.translatesAutoresizingMaskIntoConstraints = false;
self.contentView.translatesAutoresizingMaskIntoConstraints = false;
self.bodyTextView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:self.scrollView];
[self.scrollView addSubview:self.contentView];
[self.contentView addSubview:self.bodyTextView];
[self.scrollView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
[self.scrollView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;[self.scrollView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.scrollView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
[self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.leadingAnchor].active = YES;
[self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.topAnchor].active = YES;[self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.trailingAnchor].active = YES;[self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.bottomAnchor].active = YES;
[self.bodyTextView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;[self.bodyTextView.topAnchor constraintEqualToAnchor:self.subjectSeparatorView.bottomAnchor].active = YES;
[self.bodyTextView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.bodyTextView setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView.heightAnchor constraintGreaterThanOrEqualToConstant:100].active = YES;
<code>self.scrollView.translatesAutoresizingMaskIntoConstraints = false;
self.contentView.translatesAutoresizingMaskIntoConstraints = false;
self.bodyTextView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:self.scrollView];
[self.scrollView addSubview:self.contentView];
...other elements...
[self.contentView addSubview:self.bodyTextView];
...other elements...
[self.scrollView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
[self.scrollView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;[self.scrollView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.scrollView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
[self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.leadingAnchor].active = YES;
[self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.topAnchor].active = YES;[self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.trailingAnchor].active = YES;[self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.bottomAnchor].active = YES;
...other constraints...
[self.bodyTextView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;[self.bodyTextView.topAnchor constraintEqualToAnchor:self.subjectSeparatorView.bottomAnchor].active = YES;
[self.bodyTextView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.bodyTextView setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView.heightAnchor constraintGreaterThanOrEqualToConstant:100].active = YES;
...other constraints...
</code>
self.scrollView.translatesAutoresizingMaskIntoConstraints = false;
self.contentView.translatesAutoresizingMaskIntoConstraints = false;
self.bodyTextView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:self.scrollView];
[self.scrollView addSubview:self.contentView];
...other elements...
[self.contentView addSubview:self.bodyTextView];
...other elements...
[self.scrollView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
[self.scrollView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;[self.scrollView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.scrollView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
[self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.leadingAnchor].active = YES;
[self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.topAnchor].active = YES;[self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.trailingAnchor].active = YES;[self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentLayoutGuide.bottomAnchor].active = YES;
...other constraints...
[self.bodyTextView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;[self.bodyTextView.topAnchor constraintEqualToAnchor:self.subjectSeparatorView.bottomAnchor].active = YES;
[self.bodyTextView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;[self.bodyTextView setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.bodyTextView.heightAnchor constraintGreaterThanOrEqualToConstant:100].active = YES;
...other constraints...
What can I do to make my view remain scrollable, but I can access my textfield by tapping on it?
I tried putting a gestureRecognizer on my scrollview, then overriding the shouldReceiveTouch
method of the UIGestureRecognizerDelegate
. However, this does not work because the touch.view
always refers to the scrollview