简体   繁体   中英

How do I determine which UIView superview is causing the subview to have userInteractionEnabled = NO?

I have a UIView that is showing up with userInteractionEnabled = NO and I can't figure out why. The settings in Interface Builder have all the views checked for userInteractionEnabled and nothing in the code sets the user interaction to NO. If I inspect the values during debugging, however, the view and all superviews have userInteractionEnabled == NO.

I used the following code in the view controller to loop all the views and inspect their interaction status and even explicitly set the enabled value to YES:

BOOL *isEnabled = self.someView.userInteractionEnabled;
NSArray *_allViews = self.someView.window.subviews;
for (UIView *currentView in _allViews) {
    currentView.userInteractionEnabled = YES;
    isEnabled = currentView.userInteractionEnabled;
}
isEnabled = self.someView.userInteractionEnabled;

At each step in the process the isEnabled variable is always NO even though I set the user interaction to YES.

What could I be missing here?

EDIT:

Adding the debug code from @rmaddy plus a little extra I get this:

UIView *view = self.someView;
while (view) {
    isEnabled = view.userInteractionEnabled;
    if (!view.userInteractionEnabled) {
        NSLog(@"view is not enabled: %@", view);
        view.userInteractionEnabled = YES;
    }
    view = view.superview;
}

When I step through that the isEnabled variable is always NO, but the if (!view.userInteractionEnabled) logic never occurs. How can it be both? Is there something about Objective-C BOOL that I don't understand? How can the value of isEnabled be NO in the debugger yet the if (!view.userInteractionEnabled) condition never applies?

Very confused now.

Try starting at the subview and walk up the superview chain.

UIView *view = self.someView;
while (view) {
    if (!view.userInteractionEnabled) {
        NSLog(@"view is not enabled: %@", view);
        view.userInteractionEnabled = YES;
    }
    view = view.superview;
}

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