From 9435d1d0ecc6d685ddeb283a18ce7873fa2ae471 Mon Sep 17 00:00:00 2001 From: Werner Altewischer Date: Mon, 13 Jun 2011 18:57:43 +0200 Subject: [PATCH] Fixed issues #10 and #11 thanks to chrispix and ntamas --- Classes/Popover/WEPopoverContainerView.m | 48 ++++++++++++++++++++++-- Classes/Popover/WEPopoverController.m | 2 +- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Classes/Popover/WEPopoverContainerView.m b/Classes/Popover/WEPopoverContainerView.m index eacf6e3..78e3b14 100755 --- a/Classes/Popover/WEPopoverContainerView.m +++ b/Classes/Popover/WEPopoverContainerView.m @@ -45,7 +45,7 @@ - (id)initWithSize:(CGSize)theSize properties:(WEPopoverContainerViewProperties *)theProperties { if ((self = [super initWithFrame:CGRectZero])) { - self.properties = theProperties; + [self setProperties:theProperties]; correctedSize = CGSizeMake(theSize.width + properties.leftBgMargin + properties.rightBgMargin + properties.leftContentMargin + properties.rightContentMargin, theSize.height + properties.topBgMargin + properties.bottomBgMargin + properties.topContentMargin + properties.bottomContentMargin); [self determineGeometryForSize:correctedSize anchorRect:anchorRect displayArea:displayArea permittedArrowDirections:permittedArrowDirections]; @@ -278,18 +278,58 @@ - (void)determineGeometryForSize:(CGSize)theSize anchorRect:(CGRect)anchorRect d } CGRect bgFrame = CGRectOffset(theBgRect, theOffset.x, theOffset.y); - CGRect intersection = CGRectIntersection(displayArea, bgFrame); - CGFloat surface = intersection.size.width * intersection.size.height; - CGFloat minMarginLeft = CGRectGetMinX(bgFrame) - CGRectGetMinX(displayArea); + CGFloat minMarginLeft = CGRectGetMinX(bgFrame) - CGRectGetMinX(displayArea); CGFloat minMarginRight = CGRectGetMaxX(displayArea) - CGRectGetMaxX(bgFrame); CGFloat minMarginTop = CGRectGetMinY(bgFrame) - CGRectGetMinY(displayArea); CGFloat minMarginBottom = CGRectGetMaxY(displayArea) - CGRectGetMaxY(bgFrame); + if (minMarginLeft < 0) { + // Popover is too wide and clipped on the left; decrease width + // and move it to the right + theOffset.x -= minMarginLeft; + theBgRect.size.width += minMarginLeft; + minMarginLeft = 0; + if (theArrowDirection == UIPopoverArrowDirectionRight) { + theArrowRect.origin.x = CGRectGetMaxX(theBgRect) - properties.rightBgMargin; + } + } + if (minMarginRight < 0) { + // Popover is too wide and clipped on the right; decrease width. + theBgRect.size.width += minMarginRight; + minMarginRight = 0; + if (theArrowDirection == UIPopoverArrowDirectionLeft) { + theArrowRect.origin.x = CGRectGetMinX(theBgRect) - leftArrowImage.size.width + properties.leftBgMargin; + } + } + if (minMarginTop < 0) { + // Popover is too high and clipped at the top; decrease height + // and move it down + theOffset.y -= minMarginTop; + theBgRect.size.height += minMarginTop; + minMarginTop = 0; + if (theArrowDirection == UIPopoverArrowDirectionDown) { + theArrowRect.origin.y = CGRectGetMaxY(theBgRect) - properties.bottomBgMargin; + } + } + if (minMarginBottom < 0) { + // Popover is too high and clipped at the bottom; decrease height. + theBgRect.size.height += minMarginBottom; + minMarginBottom = 0; + if (theArrowDirection == UIPopoverArrowDirectionUp) { + theArrowRect.origin.y = CGRectGetMinY(theBgRect) - upArrowImage.size.height + properties.topBgMargin; + } + } + bgFrame = CGRectOffset(theBgRect, theOffset.x, theOffset.y); + CGFloat minMargin = MIN(minMarginLeft, minMarginRight); minMargin = MIN(minMargin, minMarginTop); minMargin = MIN(minMargin, minMarginBottom); + // Calculate intersection and surface + CGRect intersection = CGRectIntersection(displayArea, bgFrame); + CGFloat surface = intersection.size.width * intersection.size.height; + if (surface >= biggestSurface && minMargin >= currentMinMargin) { biggestSurface = surface; offset = theOffset; diff --git a/Classes/Popover/WEPopoverController.m b/Classes/Popover/WEPopoverController.m index 16a5734..456968b 100755 --- a/Classes/Popover/WEPopoverController.m +++ b/Classes/Popover/WEPopoverController.m @@ -37,7 +37,7 @@ @implementation WEPopoverController @synthesize passthroughViews; - (id)init { - if (self = [super init]) { + if ((self = [super init])) { } return self; }