runway20 / PopoverView

A simple UIView popover control for iPhone/iPad written in CoreGraphics.
1.02k stars 214 forks source link

Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]' #15

Closed Wirsing84 closed 11 years ago

Wirsing84 commented 11 years ago

Hello! PopoverView looks great and we'd really like to use it. However, wherever I add it to our app it crashes with a runtime error during

The crash happens at this line: self.frame = topViewBounds;

-> Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'

An NSLog I created above that line retrieves: Frame: X=nan Y=nan W=0.0 H=0.0 - TopViewBounds: X=0.0 Y=0.0 W=0.0 H=0.0

It seems like there is something wrong with creating the PopoverView's frame.

Running in iOS 5 and iOS6 Simulator.

I appreciate any ideas and help!

ocrickard commented 11 years ago

Interesting. So from this, it would appear that the PopoverView is being created (initWithFrame) with an invalid frame rect, as you suggest. I really can't imagine why this would happen, since I call initWithFrame with a frame of CGRectZero which is defined as the rect (0,0,0,0), not (NaN, NaN, 0, 0), unless your runtime has this definition overridden somewhere?

The second part that is unusual to me is the fact that the topViewBounds is CGRectZero here. This would suggest to me that your window stack is not setup as I had assumed. If you wouldn't mind, in the - (void)showAtPoint:(CGPoint)point inView:(UIView *)view withContentView:(UIView *)cView, could you check for the existence and state of topView? If it was unable to find a valid subview of either the UIApplication's keyWindow, or the top of the window stack, then you may have a bad topView var. If this is the case, try replacing the line

topView = [[window subviews] objectAtIndex:0];

with:

topView = window;

Since UIWindow is a subclass of UIView, you can actually just add the popover directly to the window, which will put it at the top of the subview stack.

It would appear that the view state is very unusual in your case. I can only think of two reasons that this may occur: 1. You are calling the PopoverView before the application's window stack has been fully initialized. 2. You are performing this method from a background thread that is unsafely accessing vars on the main thread.

These are just guesses, but if I can get more info on how you're using this, or if you could replicate the problem in the demo project, then I can try to add a workaround to the code base. If the problem is arising from background usage, I would suggest you wrap your PopoverView code in a dispatch_async on the main thread.

Wirsing84 commented 11 years ago

Thank you for your elaborate answer!

I checked the topview frame - after [[window subviews] objectAtIndex:0]; it remains a CGRectZero (0, 0, 0, 0) (I have not changed its implementation)

With topView = window; it works as it should.

I'm calling from a "normal" button via an IBAction (long after the window stack is set up). Also I am positively doing it in the Main Thread, I'm calling it from a method directly in my main View Controller.

However, I just found out what the problem is: I enumerated and logged the descriptions of the objects in [window subviews] and it returns this:

UITextView: 0xc970e00; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; gestureRecognizers = <NSArray: 0xb97ce50>; layer = CALayer: 0xb97c890; contentOffset: {0, 0}

UILayoutContainerView: 0xaabed40; frame = (0 0; 320 480); autoresize = W+H; layer = CALayer: 0xaabee00

DCFrameView: 0x12666230; frame = (0 0; 320 480); alpha = 0; opaque = NO; layer = CALayer: 0x126662c0


What is this, you say? Well, UITextView (at index 0) with frame 0 0 0 0 obviously creates the problem. And DCFrameView gives away what is happening. I am using DCIntrospect - https://github.com/domesticcatsoftware/DCIntrospect - its a great tool for debugging UI-related issues. If you push the SPACE-bar in the simulator it gives you the exact positions of all views and more great stuff. As we see, this f*cks with the window hierarchy and messes up the implementation of PopoverView. This problem will not occur when built on a device. However, I recommend a workaround as I am probably not the only one using DCIntrospect in combination with PopoverView :)

Wirsing84 commented 11 years ago

A possible workaround would be this:

for(UIView *subView in [window subviews]){
    if(!CGRectEqualToRect(subView.frame, CGRectZero)){
        topView = subView;
        break;
    }
}

It works, I'm just not 100% sure of the outcome in other window hierarchies. But it sure is safer than just using objectAtIndex:0

ocrickard commented 11 years ago

Hmm, I think it may be safest to add the PopoverView directly to the window. I was thinking about your solution, but I'm concerned that one of the view's layer.zPosition would be set to be below the view stack, resulting in an invisible PopoverView. The window should always be visible. I'll push shortly with the fix.

Wirsing84 commented 11 years ago

Sounds like a good plan :)

ocrickard commented 11 years ago

https://github.com/runway20/PopoverView/commit/ce6843cb1a09dcaf46c07c66d059707f9946d845