简体   繁体   中英

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.

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;
}

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?

确保将文本字段插入内容视图。

I had this same problem and found a solution here https://stackoverflow.com/questions/20015266

It seems GMSMapView has a BlockingGestureRecognizer which prevents the interaction with the text field. If you disable this keep in mind that the parent views will receive the events from the gestures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM