Skip to content

Commit 5abf2ae

Browse files
fix popover content size when used with an uinavigationcontroller on ios7
1 parent e9a53c3 commit 5abf2ae

3 files changed

Lines changed: 62 additions & 10 deletions

File tree

WYPopoverController/WYPopoverController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#endif
3535

3636
typedef NS_OPTIONS(NSUInteger, WYPopoverArrowDirection) {
37+
WYPopoverArrowDirectionNone = 0,
3738
WYPopoverArrowDirectionUp = 1UL << 0,
3839
WYPopoverArrowDirectionDown = 1UL << 1,
3940
WYPopoverArrowDirectionLeft = 1UL << 2,

WYPopoverController/WYPopoverController.m

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,22 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
10091009

10101010
reducedOuterCornerRadius = MIN(reducedOuterCornerRadius, outerCornerRadius);
10111011

1012+
if (arrowDirection == WYPopoverArrowDirectionNone)
1013+
{
1014+
origin = CGPointMake(CGRectGetMidX(outerRect), CGRectGetMinY(outerRect));
1015+
1016+
CGPathMoveToPoint(outerPathRef, NULL, origin.x, origin.y);
1017+
1018+
CGPathAddLineToPoint(outerPathRef, NULL, CGRectGetMaxX(outerRect) - outerCornerRadius, CGRectGetMinY(outerRect));
1019+
1020+
CGPathAddArcToPoint(outerPathRef, NULL, CGRectGetMaxX(outerRect), CGRectGetMinY(outerRect), CGRectGetMaxX(outerRect), CGRectGetMaxY(outerRect), outerCornerRadius);
1021+
CGPathAddArcToPoint(outerPathRef, NULL, CGRectGetMaxX(outerRect), CGRectGetMaxY(outerRect), CGRectGetMinX(outerRect), CGRectGetMaxY(outerRect), outerCornerRadius);
1022+
CGPathAddArcToPoint(outerPathRef, NULL, CGRectGetMinX(outerRect), CGRectGetMaxY(outerRect), CGRectGetMinX(outerRect), CGRectGetMinY(outerRect), outerCornerRadius);
1023+
CGPathAddArcToPoint(outerPathRef, NULL, CGRectGetMinX(outerRect), CGRectGetMinY(outerRect), CGRectGetMaxX(outerRect), CGRectGetMinY(outerRect), outerCornerRadius);
1024+
1025+
CGPathAddLineToPoint(outerPathRef, NULL, origin.x, origin.y);
1026+
}
1027+
10121028
if (arrowDirection == WYPopoverArrowDirectionUp)
10131029
{
10141030
origin = CGPointMake(CGRectGetMidX(outerRect) + arrowOffset - arrowBase / 2, CGRectGetMinY(outerRect));
@@ -1214,7 +1230,7 @@ - (CGRect)arrowRect:(CGRect)rect arrowDirection:(WYPopoverArrowDirection)aArrowD
12141230
{
12151231
CGRect result = CGRectZero;
12161232

1217-
if (arrowHeight > 0)
1233+
if (arrowHeight > 0 && aArrowDirection != WYPopoverArrowDirectionDown)
12181234
{
12191235
result.size = CGSizeMake(arrowBase, arrowHeight);
12201236

@@ -1414,17 +1430,27 @@ - (CGSize)contentSizeForViewInPopover
14141430
{
14151431
CGSize result = CGSizeZero;
14161432

1433+
UIViewController *controller = viewController;
1434+
1435+
UINavigationController *navigationController = (UINavigationController *)controller;
1436+
if ([[navigationController viewControllers] count] > 0)
1437+
{
1438+
controller = (UIViewController *)[[navigationController viewControllers] objectAtIndex:0];
1439+
}
1440+
14171441
#ifdef WY_BASE_SDK_7_ENABLED
1418-
if ([viewController respondsToSelector:@selector(preferredContentSize)])
1442+
if ([controller respondsToSelector:@selector(preferredContentSize)])
14191443
{
1420-
result = viewController.preferredContentSize;
1444+
result = controller.preferredContentSize;
14211445
}
14221446
#endif
1447+
14231448
if (CGSizeEqualToSize(result, CGSizeZero))
14241449
{
14251450
#pragma clang diagnostic push
14261451
#pragma GCC diagnostic ignored "-Wdeprecated"
1427-
result = viewController.contentSizeForViewInPopover;
1452+
1453+
result = controller.contentSizeForViewInPopover;
14281454
#pragma clang diagnostic pop
14291455
}
14301456

@@ -1604,7 +1630,7 @@ - (void)positionPopover
16041630

16051631
overlayView.frame = self.rootView.bounds;
16061632

1607-
viewFrame = [overlayView convertRect:rect fromView:inView];
1633+
viewFrame = (permittedArrowDirections != WYPopoverArrowDirectionNone) ? [overlayView convertRect:rect fromView:inView] : overlayView.bounds;
16081634

16091635
minX = popoverLayoutMargins.left;
16101636
maxX = self.rootView.bounds.size.width - popoverLayoutMargins.right;
@@ -1821,6 +1847,24 @@ - (void)positionPopover
18211847
containerView.arrowOffset = offset;
18221848
}
18231849

1850+
if (arrowDirection == WYPopoverArrowDirectionNone)
1851+
{
1852+
containerView.arrowDirection = WYPopoverArrowDirectionNone;
1853+
containerViewSize = [containerView sizeThatFits:contentViewSize];
1854+
1855+
containerFrame = CGRectZero;
1856+
containerFrame.size = containerViewSize;
1857+
containerFrame.size.width = MIN(maxX - minX, containerFrame.size.width);
1858+
containerFrame.size.height = MIN(maxY - minY, containerFrame.size.height);
1859+
containerView.frame = containerFrame;
1860+
1861+
containerView.center = CGPointMake(viewFrame.size.width / 2, viewFrame.size.height / 2);
1862+
1863+
containerFrame = containerView.frame;
1864+
1865+
containerView.arrowOffset = 0;
1866+
}
1867+
18241868
containerView.frame = containerFrame;
18251869

18261870
containerView.wantsDefaultContentAppearance = wantsDefaultContentAppearance;
@@ -1931,8 +1975,13 @@ - (WYPopoverArrowDirection)arrowDirectionForRect:(CGRect)aRect
19311975
{
19321976
WYPopoverArrowDirection arrowDirection = WYPopoverArrowDirectionUnknown;
19331977

1934-
NSMutableArray* areas = [NSMutableArray arrayWithCapacity:0];
1935-
WYPopoverArea* area;
1978+
if (arrowDirections == WYPopoverArrowDirectionNone)
1979+
{
1980+
arrowDirection = WYPopoverArrowDirectionNone;
1981+
}
1982+
1983+
NSMutableArray *areas = [NSMutableArray arrayWithCapacity:0];
1984+
WYPopoverArea *area;
19361985

19371986
if ((arrowDirections & WYPopoverArrowDirectionDown) == WYPopoverArrowDirectionDown)
19381987
{

demos/DemoSegue/WYPopoverDemoSegue/WYGesturesViewController.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@interface WYGesturesViewController () <WYPopoverControllerDelegate>
1313
{
14-
WYPopoverController* popoverController;
14+
WYPopoverController *popoverController;
1515
}
1616

1717
@end
@@ -20,14 +20,16 @@ @implementation WYGesturesViewController
2020

2121
- (IBAction)buttonTap:(id)sender
2222
{
23-
UIView* view = (UIView *)sender;
23+
UIView *button = (UIView *)sender;
2424

2525
UINavigationController* contentViewController = [[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"StadiumList"]];
2626

2727
popoverController = [[WYPopoverController alloc] initWithContentViewController:contentViewController];
2828
popoverController.delegate = self;
2929

30-
[popoverController presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
30+
//[popoverController presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
31+
32+
[popoverController presentPopoverFromRect:CGRectZero inView:self.view permittedArrowDirections:WYPopoverArrowDirectionNone animated:YES];
3133
}
3234

3335
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)aPopoverController

0 commit comments

Comments
 (0)