- Notifications
You must be signed in to change notification settings - Fork346
STPopup provides STPopupController, which works just like UINavigationController in popup style, for both iPhone and iPad. It's written in Objective-C and compatible with Swift.
License
kevin-lyn/STPopup
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
STPopup
providesSTPopupController
, which works just likeUINavigationController
in popup style, for both iPhone and iPad. It's written in Objective-C and compatible with Swift.
Features:
- Push/Pop view controller into/out of
STPopupController
just likeUINavigationController
. - Set navigation items through
self.navigationItem
just like using aUINavigationController
. - Support both "Form Sheet" and "Bottom Sheet" style.
- Work well with storyboard(including segue).
- Customize UI by using
UIAppearance
. - Customizable popup transition style.
- Auto-reposition of popup view when keyboard shows up, make sure your
UITextField
/UITextView
won't be covered by the keyboard. - Drag navigation bar to dismiss popup view.
- Support both portrait and landscape orientation in iPhone and iPad.
- iOS 7+.
- Compatible with Swift.
CocoaPods
pod'STPopup'
Carthage
github"kevin0571/STPopup"
Import header file
Objective-C
#import<STPopup/STPopup.h>
Swift
import STPopup
Initialize and present STPopupController
Objective-C
STPopupController *popupController = [[STPopupControlleralloc]initWithRootViewController:viewController];[popupControllerpresentInViewController:self];
Swift
letpopupController=STPopupController(rootViewController: viewController)popupController.present(in:self)
Set content size in view controller
Objective-C
@implementationViewController- (instancetype)init{if (self = [superinit]) { self.title =@"View Controller"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"Next"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(nextBtnDidTap)];// It's required to set content size of popup. self.contentSizeInPopup =CGSizeMake(300,400); self.landscapeContentSizeInPopup =CGSizeMake(400,200); }return self;}@end
Swift
classViewController:UIViewController{overrideinit(nibName nibNameOrNil:String?, bundle nibBundleOrNil:Bundle?){ super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) title="View Controller" navigationItem.rightBarButtonItem=UIBarButtonItem(title:"Next", style:.plain, target:self, action: #selector(nextBtnDidTap)) // It's required to set content size of popup. contentSizeInPopup=CGSize(width:300, height:400) landscapeContentSizeInPopup=CGSize(width:400, height:200)}}
Set content size of view controller which is loaded from Storyboard
Set content size in storyboard or inawakeFromNib
.
Push, pop and dismiss view controllers
Objective-C
[self.popupControllerpushViewController:[ViewControllernew]animated:YES];[self.popupControllerpopViewControllerAnimated:YES];// Popup will be dismissed if there is only one view controller in the popup view controller stack[self.popupControllerdismiss];
Swift
popupController?.push(viewController, animated:true)popupController?.popViewController(animated:true) // Popup will be dismissed if there is only one view controller in the popup view controller stackpopupController?.dismiss()
Bottom sheet style
Objective-C
STPopupController *popupController = [[STPopupControlleralloc]initWithRootViewController:[ViewControllernew]];popupController.style = STPopupStyleBottomSheet;[popupControllerpresentInViewController:self];
Swift
letpopupController=STPopupController(rootViewController: viewController)popupController.style=.bottomSheetpopupController.present(in:self)
Customize popup transition style
Objective-C
#pragma mark - STPopupControllerTransitioning- (NSTimeInterval)popupControllerTransitionDuration:(STPopupControllerTransitioningContext *)context{return context.action == STPopupControllerTransitioningActionPresent ?0.5 :0.35;}- (void)popupControllerAnimateTransition:(STPopupControllerTransitioningContext *)context completion:(void (^)())completion{// Popup will be presented with an animation sliding from right to left. UIView *containerView = context.containerView;if (context.action == STPopupControllerTransitioningActionPresent) { containerView.transform =CGAffineTransformMakeTranslation(containerView.superview.bounds.size.width - containerView.frame.origin.x,0); [UIViewanimateWithDuration:[selfpopupControllerTransitionDuration:context]delay:0usingSpringWithDamping:1initialSpringVelocity:1options:UIViewAnimationOptionCurveEaseInOutanimations:^{ context.containerView.transform =CGAffineTransformIdentity; }completion:^(BOOL finished) {completion(); }]; }else { [UIViewanimateWithDuration:[selfpopupControllerTransitionDuration:context]delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{ containerView.transform =CGAffineTransformMakeTranslation(-2 * (containerView.superview.bounds.size.width - containerView.frame.origin.x),0); }completion:^(BOOL finished) { containerView.transform =CGAffineTransformIdentity;completion(); }]; }}// Use custom transitioning in popup controllerSTPopupController *popupController = [[STPopupControlleralloc]initWithRootViewController:viewController];popupController.transitionStyle = STPopupTransitionStyleCustom;popupController.transitioning = self;[popupControllerpresentInViewController:self];
Swift
// MARK: STPopupControllerTransitioningfunc popupControllerTransitionDuration(_ context:STPopupControllerTransitioningContext)->TimeInterval{return context.action==.present?0.5:0.35}func popupControllerAnimateTransition(_ context:STPopupControllerTransitioningContext, completion:@escaping()->Void){ // Popup will be presented with an animation sliding from right to left.letcontainerView= context.containerViewif context.action==.present{ containerView.transform=CGAffineTransform(translationX: containerView.superview!.bounds.size.width- containerView.frame.origin.x, y:0)UIView.animate(withDuration:popupControllerTransitionDuration(context), delay:0, usingSpringWithDamping:1, initialSpringVelocity:1, options:.curveEaseInOut, animations:{ containerView.transform=.identity}, completion:{ _incompletion()});}else{UIView.animate(withDuration:popupControllerTransitionDuration(context), delay:0, usingSpringWithDamping:1, initialSpringVelocity:1, options:.curveEaseInOut, animations:{ containerView.transform=CGAffineTransform(translationX:-2*(containerView.superview!.bounds.size.width- containerView.frame.origin.x), y:0)}, completion:{ _in containerView.transform=.identitycompletion()});}}// Use custom transitioning in popup controllerletpopupController= let popupController=STPopupController(rootViewController: viewController)popupController.transitionStyle=.custompopupController.transitioning=selfpopupController.present(in:self)
Blur background
Objective-C
STPopupController *popupController = [[STPopupControlleralloc]initWithRootViewController:[PopupViewController1new]];if (NSClassFromString(@"UIBlurEffect")) { UIBlurEffect *blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark]; popupController.backgroundView = [[UIVisualEffectViewalloc]initWithEffect:blurEffect];}
Swift
letpopupController= let popupController=STPopupController(rootViewController: viewController)ifNSClassFromString("UIBlurEffect")!=nil{letblurEffect=UIBlurEffect(style:.dark) popupController.backgroundView=UIVisualEffectView(effect: blurEffect)}
Action of tapping on area outside of popup
Objective-C
[popupController.backgroundViewaddGestureRecognizer:[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(backgroundViewDidTap)]];
Swift
popupController.backgroundView?.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(backgroundViewDidTap)))
Customize UI
Objective-C
[STPopupNavigationBarappearance].barTintColor = [UIColorcolorWithRed:0.20green:0.60blue:0.86alpha:1.0];[STPopupNavigationBarappearance].tintColor = [UIColorwhiteColor];[STPopupNavigationBarappearance].barStyle = UIBarStyleDefault;[STPopupNavigationBarappearance].titleTextAttributes = @{NSFontAttributeName: [UIFontfontWithName:@"Cochin"size:18],NSForegroundColorAttributeName: [UIColorwhiteColor] }; [[UIBarButtonItemappearanceWhenContainedIn:[STPopupNavigationBarclass],nil]setTitleTextAttributes:@{NSFontAttributeName:[UIFontfontWithName:@"Cochin"size:17] }forState:UIControlStateNormal];
Swift
STPopupNavigationBar.appearance().barTintColor=UIColor(red:0.2, green:0.6, blue:0.86, alpha:1)STPopupNavigationBar.appearance().tintColor=.whiteSTPopupNavigationBar.appearance().barStyle=.defaultSTPopupNavigationBar.appearance().titleTextAttributes=[.font:UIFont(name:"Cochin", size:18)??.systemFont(ofSize:18),.foregroundColor:UIColor.white,]UIBarButtonItem.appearance(whenContainedInInstancesOf:[STPopupNavigationBar.self]).setTitleTextAttributes([.font:UIFont(name:"Cochin", size:18)??.systemFont(ofSize:18),], for:.normal)
Auto-reposition when keyboard is showing up
This is default behavior.
Drag to dismiss
This is default behavior.
Handle orientation change
This is default behavior.
Please checkout the example project for more details.
About
STPopup provides STPopupController, which works just like UINavigationController in popup style, for both iPhone and iPad. It's written in Objective-C and compatible with Swift.