@@ -25,6 +25,8 @@ of this software and associated documentation files (the "Software"), to deal
2525
2626#import " WYPopoverController.h"
2727
28+ #import < objc/runtime.h>
29+
2830#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
2931 #define WY_BASE_SDK_7_ENABLED
3032#endif
@@ -44,6 +46,63 @@ of this software and associated documentation files (the "Software"), to deal
4446#define WY_IS_IOS_LESS_THAN (v ) ([[[UIDevice currentDevice ] systemVersion ] compare: v options: NSNumericSearch] == NSOrderedAscending)
4547
4648
49+ @interface UINavigationController (WYPopover)
50+
51+ @property (nonatomic , assign , getter = isEmbedInPopover) BOOL embedInPopover;
52+
53+ @end
54+
55+ @implementation UINavigationController (WYPopover)
56+
57+ static char const * const UINavigationControllerEmbedInPopoverTagKey = " UINavigationControllerEmbedInPopoverTagKey" ;
58+
59+ @dynamic embedInPopover;
60+
61+ + (void )load
62+ {
63+ Method original, swizzle;
64+
65+ original = class_getInstanceMethod (self, @selector (pushViewController:animated: ));
66+
67+ swizzle = class_getInstanceMethod (self, @selector (sizzled_pushViewController:animated: ));
68+
69+ method_exchangeImplementations (original, swizzle);
70+ }
71+
72+ - (BOOL )isEmbedInPopover
73+ {
74+ BOOL result = NO ;
75+
76+ NSNumber *value = objc_getAssociatedObject (self, UINavigationControllerEmbedInPopoverTagKey);
77+
78+ if (value)
79+ {
80+ result = [value boolValue ];
81+ }
82+
83+ return result;
84+ }
85+
86+ - (void )setEmbedInPopover : (BOOL )value
87+ {
88+ objc_setAssociatedObject (self, UINavigationControllerEmbedInPopoverTagKey, [NSNumber numberWithBool: value], OBJC_ASSOCIATION_RETAIN_NONATOMIC );
89+ }
90+
91+ - (void )sizzled_pushViewController : (UIViewController *)viewController animated : (BOOL )animated
92+ {
93+ if (self.isEmbedInPopover && WY_IS_IOS_GREATER_THAN_OR_EQUAL_TO (@" 7.0" ))
94+ {
95+ viewController.edgesForExtendedLayout = UIRectEdgeNone;
96+ }
97+
98+ [self sizzled_pushViewController: viewController animated: animated];
99+ }
100+
101+ @end
102+
103+
104+ // //////////////////////////////////////////////////////////////////////////////////////////////////////
105+
47106@interface WYPopoverArea : NSObject
48107{
49108}
@@ -1446,7 +1505,7 @@ - (void)presentPopoverFromRect:(CGRect)aRect inView:(UIView *)aView permittedArr
14461505
14471506 [self positionPopover ];
14481507
1449- [self drawPopoverNavigationBar ];
1508+ [self setPopoverNavigationBarBackgroundImage ];
14501509
14511510 containerView.hidden = NO ;
14521511
@@ -1499,85 +1558,19 @@ - (void)presentPopoverFromRect:(CGRect)aRect inView:(UIView *)aView permittedArr
14991558 }
15001559}
15011560
1502- - (void )drawPopoverNavigationBar
1561+ - (void )setPopoverNavigationBarBackgroundImage
15031562{
15041563 if (wantsDefaultContentAppearance == NO && [viewController isKindOfClass: [UINavigationController class ]])
15051564 {
1506- // UIBarMetricsDefault > navigationBarHeight = 44
1507- // UIBarMetricsLandscapePhone > navigationBarHeight = 32
1508-
15091565 UINavigationController *navigationController = (UINavigationController *)viewController;
1566+ navigationController.embedInPopover = YES ;
15101567
1511- // UIBarMetricsDefault
1512-
1513- CGFloat navigationBarHeight = 44 ;
1514-
1515- CGRect rectPathRect = CGRectMake (0 , 0 , 20 , navigationBarHeight);
1516-
1517- CGFloat cornerRadius = containerView.innerCornerRadius ;
1518-
1519- UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect: rectPathRect byRoundingCorners: UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii: CGSizeMake (cornerRadius, cornerRadius)];
1520-
1521- UIGraphicsBeginImageContextWithOptions (rectPath.bounds .size , NO , 0 .0f );
1522-
1523- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
1524- CGContextRef context = UIGraphicsGetCurrentContext ();
1525-
1526- // // Gradient Declarations
1527- NSArray * fillGradientColors = [NSArray arrayWithObjects:
1528- (id )containerView.fillTopColor.CGColor,
1529- (id )containerView.fillBottomColor.CGColor,
1530- nil ];
1531-
1532- CGFloat fillGradientLocations[] = {0 , 1 };
1533-
1534- CGGradientRef fillGradient = CGGradientCreateWithColors (colorSpace, (__bridge CFArrayRef)fillGradientColors, fillGradientLocations);
1535-
1536- // // Drawing
1537- CGContextSaveGState (context);
1568+ if (WY_IS_IOS_GREATER_THAN_OR_EQUAL_TO (@" 7.0" ) && [navigationController viewControllers ] && [[navigationController viewControllers ] count ] > 0 )
15381569 {
1539- // CGContextAddPath(context, [rectPath CGPath]);
1540- // CGContextClip(context);
1541-
1542- [rectPath addClip ];
1543-
1544- CGFloat gradientTopPosition = [containerView innerRect ].origin .y - navigationBarHeight + containerView.outerShadowInsets .top ;
1545- CGFloat gradientBottomPosition = gradientTopPosition + [containerView outerRect ].size .height ;
1546-
1547- CGContextDrawLinearGradient (context, fillGradient,
1548- CGPointMake (0 , gradientTopPosition),
1549- CGPointMake (0 , gradientBottomPosition),
1550- 0 );
1570+ [(UIViewController *)[[navigationController viewControllers ] objectAtIndex: 0 ] setEdgesForExtendedLayout: UIRectEdgeNone];
15511571 }
1552- CGContextRestoreGState (context);
1553-
1554- UIImage *image = UIGraphicsGetImageFromCurrentImageContext ();
1555- UIGraphicsEndImageContext ();
1556-
1557- CGGradientRelease (fillGradient);
1558- CGColorSpaceRelease (colorSpace);
1559-
1560- [navigationController.navigationBar setBackgroundImage: [image resizableImageWithCapInsets: UIEdgeInsetsMake (0 , cornerRadius + 1 , 0 , cornerRadius + 1 )] forBarMetrics: UIBarMetricsDefault];
1561-
1562- // UIBarMetricsLandscapePhone
1563-
1564- navigationBarHeight = 32 ;
1565-
1566- rectPath = [UIBezierPath bezierPathWithRect: CGRectMake (0 , 0 , 20 , navigationBarHeight)];
1567-
1568- UIGraphicsBeginImageContextWithOptions (rectPath.bounds .size , NO , 0 .0f );
15691572
1570- context = UIGraphicsGetCurrentContext ();
1571-
1572- CGContextClipToRect (context, CGRectMake (0 , 0 , rectPath.bounds .size .width , rectPath.bounds .size .height ));
1573-
1574- [image drawInRect: rectPath.bounds];
1575-
1576- image = UIGraphicsGetImageFromCurrentImageContext ();
1577-
1578- UIGraphicsEndImageContext ();
1579-
1580- [navigationController.navigationBar setBackgroundImage: [image resizableImageWithCapInsets: UIEdgeInsetsMake (0 , cornerRadius + 1 , 0 , cornerRadius + 1 )] forBarMetrics: UIBarMetricsLandscapePhone];
1573+ [navigationController.navigationBar setBackgroundImage: [UIImage imageWithColor: [UIColor clearColor ]] forBarMetrics: UIBarMetricsDefault];
15811574 }
15821575
15831576 viewController.view .clipsToBounds = YES ;
@@ -1586,10 +1579,6 @@ - (void)drawPopoverNavigationBar
15861579 {
15871580 viewController.view .layer .cornerRadius = containerView.outerCornerRadius ;
15881581 }
1589- else
1590- {
1591- viewController.view .layer .cornerRadius = containerView.innerCornerRadius ;
1592- }
15931582}
15941583
15951584- (void )presentPopoverFromBarButtonItem : (UIBarButtonItem *)item permittedArrowDirections : (WYPopoverArrowDirection)arrowDirections animated : (BOOL )aAnimated
@@ -1878,7 +1867,9 @@ - (void)dismissPopoverAnimated:(BOOL)aAnimated callDelegate:(BOOL)callDelegate
18781867 object: nil ];
18791868
18801869 [[UIDevice currentDevice ] endGeneratingDeviceOrientationNotifications ];
1881- [[NSNotificationCenter defaultCenter ] removeObserver: self name: UIDeviceOrientationDidChangeNotification object: nil ];
1870+ [[NSNotificationCenter defaultCenter ] removeObserver: self
1871+ name: UIDeviceOrientationDidChangeNotification
1872+ object: nil ];
18821873
18831874 [[NSNotificationCenter defaultCenter ] removeObserver: self
18841875 name: UIKeyboardWillShowNotification
0 commit comments