STPopup 1.8.7

STPopup 1.8.7

测试已测试
语言语言 obj-CObjective C
许可证 MIT
发布最后发布2019年6月

Kevin Lin维护。



STPopup 1.8.7

  • Kevin Lin

STPopup CI Status Version License

STPopup 提供了 STPopupController,其功能类似于弹出样式中的 UINavigationController,适用于 iPhone 和 iPad。它使用 Objective-C 编写,与 Swift 兼容。

功能

  • 将视图控制器推入/弹出 STPopupController,就像将视图控制器推入/弹出 UINavigationController
  • 通过 self.navigationItem 设置导航项,就像使用 UINavigationController
  • 支持“表单表”和“底部表”样式。
  • 与 storyboard(包括 segue)很好地工作。
  • 通过使用 UIAppearance 来自定义 UI。
  • 可自定义弹出转场样式。
  • 当键盘弹出时自动重新定位弹出视图,确保您的 UITextField/UITextView不会被键盘覆盖。
  • 拖动导航栏以关闭弹出视图。
  • 支持 iPhone 和 iPad 的竖屏和横屏方向。
  • iOS 7+
  • 与 Swift 兼容。

用例

Use Cases

开始使用

CocoaPods

pod 'STPopup'

Carthage

github "kevin0571/STPopup"

导入头文件
Objective-C

#import <STPopup/STPopup.h>

Swift

import STPopup

初始化并展示STPopupController
Objective-C

STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:viewController];
[popupController presentInViewController:self];

Swift

let popupController = let popupController = STPopupController(rootViewController: viewController)
popupController.present(in: self)

在视图中设置内容大小
Objective-C

@implementation ViewController

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"View Controller";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@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

class ViewController: UIViewController {
    override init(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)
    }
}

设置从Storyboard加载的视图控制器的内容大小
在Storyboard或awakeFromNib中设置内容大小。
Storyboard

推送、弹出和关闭视图控制器
Objective-C

[self.popupController pushViewController:[ViewController new] animated:YES];
[self.popupController popViewControllerAnimated:YES]; // Popup will be dismissed if there is only one view controller in the popup view controller stack
[self.popupController dismiss];

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 stack
popupController?.dismiss()

Push & Pop

底部抽屉样式
Objective-C

STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:[ViewController new]];
popupController.style = STPopupStyleBottomSheet;
[popupController presentInViewController:self];

Swift

let popupController = STPopupController(rootViewController: viewController)
popupController.style = .bottomSheet
popupController.present(in: self)

Bottom Sheet

自定义弹出转场样式
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);
        
        [UIView animateWithDuration:[self popupControllerTransitionDuration:context] delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            context.containerView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            completion();
        }];
    }
    else {
        [UIView animateWithDuration:[self popupControllerTransitionDuration:context] delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            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 controller
STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:viewController];
popupController.transitionStyle = STPopupTransitionStyleCustom;
popupController.transitioning = self;
[popupController presentInViewController:self];

Swift

// MARK: STPopupControllerTransitioning

func 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.
    let containerView = context.containerView
    if 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: { _ in
            completion()
        });
    } 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 = .identity
            completion()
        });
    }
}

// Use custom transitioning in popup controller
let popupController = let popupController = STPopupController(rootViewController: viewController)
popupController.transitionStyle = .custom
popupController.transitioning = self
popupController.present(in: self)

模糊背景
Objective-C

STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:[PopupViewController1 new]];
if (NSClassFromString(@"UIBlurEffect")) {
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    popupController.backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
}

Swift

let popupController = let popupController = STPopupController(rootViewController: viewController)
if NSClassFromString("UIBlurEffect") != nil {
    let blurEffect = UIBlurEffect(style: .dark)
    popupController.backgroundView = UIVisualEffectView(effect: blurEffect)
}

在弹出窗外点击区域的动作
Objective-C

[popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundViewDidTap)]];

Swift

popupController.backgroundView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backgroundViewDidTap)))

自定义UI
Objective-C

[STPopupNavigationBar appearance].barTintColor = [UIColor colorWithRed:0.20 green:0.60 blue:0.86 alpha:1.0];
[STPopupNavigationBar appearance].tintColor = [UIColor whiteColor];
[STPopupNavigationBar appearance].barStyle = UIBarStyleDefault;
[STPopupNavigationBar appearance].titleTextAttributes = @{ NSFontAttributeName: [UIFont fontWithName:@"Cochin" size:18], NSForegroundColorAttributeName: [UIColor whiteColor] };
    
[[UIBarButtonItem appearanceWhenContainedIn:[STPopupNavigationBar class], nil] setTitleTextAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"Cochin" size:17] } forState:UIControlStateNormal];

Swift

STPopupNavigationBar.appearance().barTintColor = UIColor(red: 0.2, green: 0.6, blue: 0.86, alpha: 1)
STPopupNavigationBar.appearance().tintColor = .white
STPopupNavigationBar.appearance().barStyle = .default
STPopupNavigationBar.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)

Customize UI

当键盘显示时自动重新定位
这是默认行为。
Auto-reposition

拖动来关闭
这是默认行为。
Drag to dismiss

处理方向变化
这是默认行为。
Orientation change

请查看示例项目获取更多详情。