• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何修改 PKRevealController 滑出菜单以处理 iOS 7?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:24:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个使用 PKRevealController 的应用程序它实现了一个滑出式菜单,类似于 iOS 上流行的 Facebook 和 GMAIL 应用程序中的菜单。该应用程序是在 XCode 5 中构建的,并在 iOS 6 和 iOS 7 上运行。我需要弄清楚如何让它在这两个地方都正常工作,所以一个简单的 .XIB hack 让它在 iOS 7 中看起来不错,但让它看起来在 iOS 6 中更糟糕的是不行。

该代码适用于 iOS 6,其中状态栏是不透明的,顶 View 未与状态栏混合使用 alpha。

但是,例如,在 iOS 7 上,我在我的 .xib 文件中创建了这个 View ,这是它在 ios 6 模拟器中运行时的显示方式,此处显示的是打开滑出菜单:

enter image description here

同样的 .xib 文件在 ios 7 上运行,当滑出菜单打开时,滑出菜单的 .xib 内容的顶部现在位于状态栏下方,正如 Apple 在其 ios 7 过渡指南中所说的那样:

enter image description here

我需要在 PKRevealController 中修改的类可能是正在创建和呈现包含 View 的呈现 View Controller ,我认为包含的 View 称为 PKRevealControllerContainerView。我想我可能需要创建 像这样的某种 View 层次结构:

   [  Outermost View container 
        [ some kind of blob to occupy the header area ]
        [ the client view I want to appear the way it did in iOS 6]
   ]

我一直在阅读,可能有更简单的方法,但我不太了解它们,例如向我的 info.plist 添加属性的方法,例如 查看基于 Controller 的状态栏外观 = 。我试过了,没有达到预期的效果。

我该如何解决这个问题?我已阅读 Fine Guide由 Apple 发布,它没有提供代码,只有像 status bar 上的这个页面这样的一般指导。 .

复制这个问题很容易,只需克隆 git repo https://github.com/pkluz/PKRevealController,构建并运行。

弹出 View 的代码如下所示:

- (void)addLeftViewControllerToHierarchy
{
    if (self.leftViewController != nil && ![self.childViewControllers containsObject:self.leftViewController])
    {
        [self addChildViewController:self.leftViewController];
        self.leftViewContainer.viewController = self.leftViewController;

        if (self.leftViewContainer == nil)
        {
            self.leftViewContainer = [[PKRevealControllerContainerView alloc] initForController:self.leftViewController shadow:NO];
            self.leftViewContainer.autoresizingMask = [self autoresizingMaskForLeftViewContainer];
        }

        self.leftViewContainer.frame = [self leftViewFrame];
        [self.view insertSubview:self.leftViewContainer belowSubview:self.frontViewContainer];
        [self.leftViewController didMoveToParentViewController:self];
    }
}

上面是由PKRevealController.m调用的,像这样:

- (void)showLeftViewControllerAnimatedBOOL)animated
                            completionPKDefaultCompletionHandler)completion
{
    __weak PKRevealController *weakSelf = self;

    void (^showLeftViewBlock)(BOOL finished) = ^(BOOL finished)
    {
        [weakSelf removeRightViewControllerFromHierarchy];
        [weakSelf addLeftViewControllerToHierarchy];  // HELLO LEFT Slide-out menu.

        ....

有比我的想法更好的方法吗? Apple 是否提供了一些方法来简化此操作,或者尝试在单个代码库中支持 iOS 6 和 iOS 7 是否让我做上述我正在考虑的黑客行为?

例如,这里是一个非常丑陋的 hack,我懒得在苹果系统状态栏下方放置任何 View ,在顶部留下一个黑条,这不好,但它表明我正在修改代码中的正确区域,至少:

- (void)addLeftViewControllerToHierarchy
{
    CGRect   lvFrame;

    if (self.leftViewController != nil && ![self.childViewControllers containsObject:self.leftViewController])
    {
        [self addChildViewController:self.leftViewController];
        self.leftViewContainer.viewController = self.leftViewController;

        if (self.leftViewContainer == nil)
        {
            self.leftViewContainer = [[PKRevealControllerContainerView alloc] initForController:self.leftViewController shadow:NO];
            self.leftViewContainer.autoresizingMask = [self autoresizingMaskForLeftViewContainer];
        }

        lvFrame = [self leftViewFrame];
        lvFrame.origin.y += 20; // ugly hack demo code only! don't really do it this badly!
        lvFrame.size.height -= 20;
        self.leftViewContainer.frame = lvFrame;
        [self.view insertSubview:self.leftViewContainer belowSubview:self.frontViewContainer];
        [self.leftViewController didMoveToParentViewController:self];
    }
}

如果我也将它添加到 UIViewController+PKRevealController.m,上面的 hack 就足够了:

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleBlackOpaque;
}

上述代码在添加时会导致以下提示/警告:

Category 正在实现一个方法,该方法也将由其主类实现。

我将上述说明包括在内以展示我的尝试,我欢迎了解真正的专家是如何做到这一点的。

我自己修改后的 PKRevealController 代码副本,包括上面的 hack,以略微改进的形式,可在此处找到:https://github.com/wpostma/PKRevealController



Best Answer-推荐答案


我也一直在为 PKRevealController 苦苦挣扎。虽然我仍在寻找更好的解决方案,但我会分享我到现在为止的想法。

我的两个问题是:

  1. 状态栏样式始终相同,我希望前 View 和菜单的样式不同;
  2. 菜单 View 顶部单元格(它是一个表格 View Controller )显示在状态栏后面。

1。动态状态栏样式

首先我有我自己的 PKRevealController 子类,我有一个自定义初始化程序和一些自定义方法来将新 View Controller 加载到前 View 导航 View Controller 中。但这暂时无关紧要。

我使用这个子类来实现 preferredStatusBarStyle 如下,以便显示 Controller 可以为每个状态提供正确的样式:

- (UIStatusBarStyle)preferredStatusBarStyle {
    switch (self.state) {
        case PKRevealControllerFocusesLeftViewController:
            return [self.leftViewController preferredStatusBarStyle];
            break;
        case PKRevealControllerFocusesRightViewController:
            return [self.rightViewController preferredStatusBarStyle];
            break;
        case PKRevealControllerFocusesFrontViewController:
            return [self.frontViewController preferredStatusBarStyle];
            break;
        case PKRevealControllerFocusesLeftViewControllerInPresentationMode:
            return [self.leftViewController preferredStatusBarStyle];
            break;
        case PKRevealControllerFocusesRightViewControllerInPresentationMode:
            return [self.rightViewController preferredStatusBarStyle];
            break;

        default:
            return UIStatusBarStyleDefault;
            break;
    }
}

但是,仅此一项是行不通的。还是要说状态栏样式需要用setNeedsStatusBarAppearanceUpdate来改变。正如 Apple 所说,这应该从动画循环内部调用,您可以在 PKRevealController 的 setFrontViewFrameLinearly 方法中找到一个。这是我修改后的样子:

- (void)setFrontViewFrameLinearlyCGRect)frame
                         animatedBOOL)animated
                         durationCGFloat)duration
                          optionsUIViewAnimationOptions)options
                       completionPKDefaultCompletionHandler)completion
{
    [UIView animateWithDuration:duration delay:0.0f optionsptions animations:^
    {
        self.frontViewContainer.frame = frame;
        if ([[[UIDevice currentDevice] systemVersion] compare"7.0" options:NSNumericSearch] != NSOrderedAscending) {
            [self setNeedsStatusBarAppearanceUpdate];
        }
    }
    completion:^(BOOL finished)
    {
        safelyExecuteCompletionBlockOnMainThread(completion, finished);
    }];
}

如果你在这一点上尝试一下,样式将会混淆。您可以很快得出结论,当 preferredStatusBarStyle 被调用时,显示 Controller 状态仍然没有改变。为此,请转到设置状态的每个方法,例如enterPresentationModeForRightViewControllerAnimated 并在它调用对帧的任何更改之前设置状态(将触发动画循环)。我在 5 个不同的地方做过。

2。带插图的左/右菜单

对于这个我不得不说我使用了一种解决方法:我刚刚在表格 View 上设置了一个标题 View (tableHeaderView 属性)。

把它放在你的 UITableViewController 的 viewDidLoad 中:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.tableView.frame.size.width, 20.f)];
headerView.backgroundColor = [UIColor whiteColor];

self.tableView.tableHeaderView = headerView;

不要忘记添加一些条件,这样它就不会在 iOS 6 中执行。使用 this other answer知道该怎么做。

关于ios - 如何修改 PKRevealController 滑出菜单以处理 iOS 7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228603/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap