Last active
March 20, 2016 16:44
-
-
Save onmyway133/b62f79b885a71c41a22a to your computer and use it in GitHub Desktop.
FTGPopoverController, a popover that works on both iOS 7 and iOS 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//FTGPopoverController.h | |
@interface FTGPopoverController : NSObject | |
- (instancetype)initWithContentVC:(UIViewController *)contentVC contentSize:(CGSize)contentSize; | |
- (void)presentInVC:(UIViewController *)vc fromButton:(UIButton *)button; | |
- (void)dismiss; | |
- (BOOL)isVisible; | |
@end | |
//FTGPopoverController.m | |
#import "FTGPopoverController.h" | |
#import <WYPopoverController/WYPopoverController.h> | |
@interface FTGPopoverController () <UIPopoverPresentationControllerDelegate> | |
@property (nonatomic, strong) UIViewController *contentVC; | |
@property (nonatomic, assign) CGSize contentSize; | |
@property (nonatomic, strong) WYPopoverController *wyPopoverController; | |
@end | |
@implementation FTGPopoverController | |
- (instancetype)initWithContentVC:(UIViewController *)contentVC contentSize:(CGSize)contentSize | |
{ | |
self = [super init]; | |
if (self) { | |
_contentVC = contentVC; | |
_contentSize = contentSize; | |
if (IOS8_OR_ABOVE) { | |
contentVC.modalPresentationStyle = UIModalPresentationPopover; | |
contentVC.preferredContentSize = contentSize; | |
} else { | |
_wyPopoverController = [[WYPopoverController alloc] initWithContentViewController:contentVC]; | |
contentVC.contentSizeForViewInPopover = contentSize; | |
} | |
} | |
return self; | |
} | |
#pragma mark - Public Interface | |
- (void)presentInVC:(UIViewController *)vc fromButton:(UIButton *)button | |
{ | |
if (IOS8_OR_ABOVE) { | |
UIPopoverPresentationController *popoverController = self.contentVC.popoverPresentationController; | |
popoverController.delegate = self; | |
popoverController.sourceRect = button.bounds; | |
popoverController.sourceView = button; | |
popoverController.permittedArrowDirections = UIPopoverArrowDirectionAny; | |
[vc presentViewController:self.contentVC animated:YES completion:nil]; | |
} else { | |
[self.wyPopoverController presentPopoverFromRect:button.bounds | |
inView:button | |
permittedArrowDirections:WYPopoverArrowDirectionAny | |
animated:YES]; | |
} | |
} | |
- (void)dismiss | |
{ | |
if (IOS8_OR_ABOVE) { | |
[self.contentVC dismissViewControllerAnimated:YES completion:nil]; | |
} else { | |
[self.wyPopoverController dismissPopoverAnimated:YES]; | |
} | |
} | |
- (BOOL)isVisible | |
{ | |
if (IOS8_OR_ABOVE) { | |
return self.contentVC.presentingViewController ? YES : NO; | |
} else { | |
return self.wyPopoverController.isPopoverVisible; | |
} | |
} | |
#pragma mark - UIPopoverPresentationControllerDelegate | |
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller | |
{ | |
return UIModalPresentationNone; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment