Created
September 3, 2016 14:09
-
-
Save KalpeshTalkar/1a942c1abc7d5bb6a14143235d86032f to your computer and use it in GitHub Desktop.
A UIViewController category to present viewcontroller as popover controller in iPhone devices
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
// | |
// UIViewController+PresentationStyle.h | |
// | |
// Created by Kalpesh Talkar on 22/08/16. | |
// Copyright © 2016 Kalpesh Talkar. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIViewController (PresentationStyle) <UIPopoverPresentationControllerDelegate> | |
- (void)presentViewController:( UIViewController * _Nonnull)viewController fromBarButtonItem:(UIBarButtonItem * _Nonnull)barButtonItem animated:(BOOL)animated completion:(void (^ __nullable)(void))completion; | |
- (void)presentViewController:(UIViewController * _Nonnull)viewController fromView:(UIView * _Nonnull)source animated:(BOOL)animated completion:(void (^ __nullable)(void))completion; | |
@end |
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
// | |
// UIViewController+PresentationStyle.m | |
// | |
// Created by Kalpesh Talkar on 22/08/16. | |
// Copyright © 2016 Kalpesh Talkar. All rights reserved. | |
// | |
#import "UIViewController+PresentationStyle.h" | |
@implementation UIViewController (PresentationStyle) | |
#pragma mark - Style | |
- (void)setStyle:(UIModalPresentationStyle)presentationStyle :(UIModalTransitionStyle)transitionStyle { | |
[self setModalPresentationStyle:presentationStyle]; | |
[self setModalTransitionStyle:transitionStyle]; | |
} | |
#pragma mark - Present methods | |
- (void)presentViewController:( UIViewController * _Nonnull)viewController fromBarButtonItem:(UIBarButtonItem * _Nonnull)barButtonItem animated:(BOOL)animated completion:(void (^ __nullable)(void))completion { | |
[self configurePopoverController:viewController withSource:barButtonItem]; | |
[self presentViewController:viewController animated:animated completion:completion]; | |
} | |
- (void)presentViewController:(UIViewController * _Nonnull)viewController fromView:(UIView * _Nonnull)source animated:(BOOL)animated completion:(void (^ __nullable)(void))completion { | |
[self configurePopoverController:viewController withSource:source]; | |
[self presentViewController:viewController animated:animated completion:completion]; | |
} | |
#pragma mark - Configuration | |
- (void)configurePopoverController:(UIViewController *)viewController withSource:(id)source { | |
[viewController setStyle:UIModalPresentationPopover :UIModalTransitionStyleCrossDissolve]; | |
UIPopoverPresentationController *popoverController = viewController.popoverPresentationController; | |
popoverController.delegate = self; | |
if ([source isKindOfClass:[UIBarButtonItem class]]) { | |
popoverController.barButtonItem = source; | |
} else { | |
popoverController.sourceView = source; | |
CGRect frame = ((UIView *)source).frame; | |
popoverController.sourceRect = CGRectMake(frame.size.width/2, frame.size.height/4, frame.size.width, frame.size.height); | |
} | |
[popoverController setPermittedArrowDirections:UIPopoverArrowDirectionAny]; | |
} | |
#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