Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField within UIView not responding to user interaction

Here's my scenario:

I have a UIViewController in my StoryBoard for my mapping feature. I'm adding a Google Map view (GoogleMaps iOS 1.3.1) like:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
   longitude:151.20 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 49, 320, 450) camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;

I created a custom view: MapTopBar.xib. Simply put, it's a UIView with an UIImageView (background) and a UITextField & Button. It has a backing class file and all of the UITextFields properties have been set (delegate, outlets, etc).

I'm adding my custom view to my Map VC like:

UIView *topBar = [[MapTopBar alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
[self.view addSubview:topBar];

My custom view is being added to the main view, but the UITextField is not receiving any touch events. The keyboard is not being displayed, no cursor in the field, nothing. It's as if something is covering it up, not allowing any user interaction. The button next to it can be pressed and works fine.

I've tried doing this many different ways (creating the entire topBar view programmatically, adding the text field programmatically) and no matter what, the text field will not get user interaction.

I've enabled user interaction, made sure no view was on top of the text field, all for null. I feel like I'm missing something simple. Why can't I interact with my textfield?

Also: in case it's relevant, the code above is within it's own sub being called from the viewDidLoad before the super call.

like image 839
bschultz Avatar asked Nov 03 '22 19:11

bschultz


2 Answers

It seems likely that the UITextField is outside the bounds of its parent view, and not being clipped. (Or the parent view is outside the bounds of its parent... etc.)

In viewDidAppear, use the debugger or NSLog the view hierarchy that ends in your UITextField. Check to ensure that the frames of each view are fully within their parent view.

Something like this should do the trick to identify any out-of-frame views:

UIView* v = _myTextField;
while ( v.superview != nil )
{
    NSLog( @"%@ - %@", NSStringFromClass([v class]), CGRectContainsRect( v.superview.bounds, v.frame ) ? @"GOOD!" : @"BAD!" );
    v = v.superview;
}
like image 161
TomSwift Avatar answered Nov 15 '22 07:11

TomSwift


I have the same issue! Have you found any work around? Seems that everything is ok with UITextField's frame. For debug purpose I've put a button above the UITextField and this button was able to get touches. Maybe the deal is in the way the Google map view handles touches or ui drawing?

like image 42
charley_chaplin Avatar answered Nov 15 '22 05:11

charley_chaplin