如何在UINavigationBar下面添加一个视图

Eth*_*ick 2 iphone uinavigationbar uinavigationcontroller ios ios6

好吧,在深入深渊(即iOS私人课程)和最终可重复使用代码的讨伐之间存在着一条很好的界限.

我想在UINavigationBar下面有一个任意UIView,大约40像素高.现在,通常这可以在封闭的视图中抛出,但我希望这个UIView 在UINavigationBar中.因此,当用户向下钻取到不同视图时,导航栏下方的UIView将保持存在.

我已经尝试构建一个容器UIViewController,它将被放置为UINavigationController中的RootViewController,但是这没有用,因为当用户推送新视图时,我的容器视图被滑出,而我的持久性栏顺其自然.

如果我在UIViewController中放置一个UINavigationController,我可以覆盖我想要的UIView条,但内部视图似乎仍然没有正确地改变它们的框架,以及当从导航堆栈推出或弹出视图时,由于UINavigationController不知道屏幕下方的新Y位置,因此视图动画效果不佳(更改其Y位置以及移出屏幕.)

所以,我研究了UINavigationController的子类化.我知道,它说" 这个类不是用于子类化的. ",但总有例外.这个子类可以简单地在导航栏下面添加持久性UIView,调整内容部分的大小(普通视图将出现的位置),一切都会正常工作.

但是在使用ObjC运行时类之后,我找不到调整的魔术变量来使其工作(UINavigationController的_containerView就是这样一个变量).

最好的方法是什么?有没有比复制和粘贴代码更好的方法,使这个Bar成为将显示它的所有ViewControllers?

Jai*_*ani 5

我猜你正在寻找一个上传进度条或类似的东西显示在导航栏下面,无论你去哪个视图,它都会留在你身边.我刚刚在几周前实现了这一点,并提出了一个额外的警告 - 我们正在使用IIViewDeck,因此我们必须能够处理整个UINavigationController的更改.

我做的是我将视图添加为导航栏的子视图.我的导航栏和导航控制器都是子类(并且应用程序已经提交到App Store而没有发生任何事故).起初我这样做是因为我需要在没有帮助的情况下自定义导航栏,UIAppearance但现在它似乎是继续给予的礼物,因为它为我提供了相当多的灵活性.

  1. 创建自定义的UINavigationController和UINavigationBar
  2. 如果你需要处理触摸,你必须实现hitTest:withEvent- 我也包括下面的代码.
  3. 如果您有一个更改UINavigationController视图的应用程序(例如标签栏应用程序,或者侧面菜单允许您更改UINavigationController中心的那个),我实施了KVO来监控中心视图控制器,以便进度视图可以"跟随"它周围.代码也在下面.

这就是我创建自定义UINavigationController和UINavigationBar的方法:

+ (ZNavigationController *)customizedNavigationController
{
 //This is just a regular UINavigationBar subclass - doesn't have to have any code but I personally override the pop/push methods to call my own flavor of viewWill/Did/Appear/Disappear methods - iOS has always been a bit finicky about what gets called when
    ZNavigationController *navController = [[ZNavigationController alloc] initWithNibName:nil bundle:nil];

    // Ensure the UINavigationBar is created so that it can be archived. If we do not access the
    // navigation bar then it will not be allocated, and thus, it will not be archived by the
    // NSKeyedArchvier.
    [navController navigationBar];

    // Archive the navigation controller.
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:navController forKey:@"root"];
    [archiver finishEncoding];

    // Unarchive the navigation controller and ensure that our UINavigationBar subclass is used.
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    [unarchiver setClass:[ZNavigationBar class] forClassName:@"UINavigationBar"];
    ZNavigationController *customizedNavController = [unarchiver decodeObjectForKey:@"root"];
    [unarchiver finishDecoding];

    // Modify the navigation bar to have a background image.
    ZNavigationBar *navBar = (ZNavigationBar *)[customizedNavController navigationBar];

    [navBar setTintColor:kZodioColorBlue];
    [navBar setBackgroundImage:[UIImage imageNamed:@"Home_TopBar_RoundCorner_withLogo"] forBarMetrics:UIBarMetricsDefault];

    return customizedNavController;
}
Run Code Online (Sandbox Code Playgroud)

现在您有了自己的自定义ZNavigationBar(这些是我的代码中的实际名称 - 很难完成并彻底改变它们),您实现了触摸处理 - 变量名称非常自我解释 - 我想检测'左边的触摸此进度条的附件视图','主要区域'或"右侧附件视图":

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    DDLogVerbose(@"Got a touch at point X: %f Y: %f", point.x, point.y);

 //First check if the progress view is visible and touch is within that frame
    if ([[JGUploadManager sharedManager] progressViewVisible] &&
        CGRectContainsPoint([[JGUploadManager sharedManager] uploadProgressView].frame, point))
    {
 //Modified frame - have to compensate for actual position of buttons in the view from the standpoint of the UINavigationBar. Can probably use another/smarter method to do this.
        CGRect modifiedCancelButtonFrame = [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryFrame;
        modifiedCancelButtonFrame.origin.y += [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView.superview.frame.origin.y;

 //Do the same thing for the right view
        CGRect modifiedRetryButtonFrame = [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryFrame;
        modifiedRetryButtonFrame.origin.y += [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView.superview.frame.origin.y;


 //Touch is on the left button
        if ([[JGUploadManager sharedManager] progressViewVisible] &&
            [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView &&
            CGRectContainsPoint(modifiedCancelButtonFrame, point))
        {
            return [[JGUploadManager sharedManager] uploadProgressView].leftAccessoryView;
        }


 //Touch is on the right button
        else if ([[JGUploadManager sharedManager] progressViewVisible] &&
                 [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView &&
                 CGRectContainsPoint(modifiedRetryButtonFrame, point))
        {
            return [[JGUploadManager sharedManager] uploadProgressView].rightAccessoryView;
        }

 //Touch is in the main/center area
        else
        {
            return [[JGUploadManager sharedManager] uploadProgressView];
        }

    }

 //Touch is not in the progress view, pass to super
       else
    {
        return [super hitTest:point withEvent:event];
    }
}
Run Code Online (Sandbox Code Playgroud)

现在是"跟随"导航控制器的KVO部件.在某个地方实现KVO - 我使用单独的'上传管理器'类并将其放在该类的init中,但这取决于您的应用程序的设计:

ZRootViewController *rootViewController = ((JGAppDelegate*)[UIApplication sharedApplication].delegate).rootViewDeckController;
[rootViewController addObserver:sharedManager
                     forKeyPath:@"centerController"
                        options:NSKeyValueObservingOptionNew
                        context:nil];
Run Code Online (Sandbox Code Playgroud)

然后当然实现这个:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    DDLogVerbose(@"KVO shows something changed: %@", keyPath);

    if ([keyPath isEqualToString:@"centerController"])
    {
        DDLogVerbose(@"Center controller changed to: %@", object);
        [self centerViewControllerChanged];
    }

}
Run Code Online (Sandbox Code Playgroud)

最后centerViewControllerChanged功能:

- (void)centerViewControllerChanged
{
    ZRootViewController *rootViewController = ((JGAppDelegate*)[UIApplication sharedApplication].delegate).rootViewDeckController;

    ZNavigationController *centerController = (ZNavigationController *)rootViewController.centerController;

    //Check if the upload progress view is visible and/or active
    if (self.uploadProgressView.frame.origin.y > 0 && self.uploadProgressView.activityIndicatorView.isAnimating)
    {
        [centerController.navigationBar addSubview:self.uploadProgressView];
    }

    //Keep weak pointer to center controller for other stuff
    DDLogVerbose(@"Setting center view controller: %@", centerController);
    self.centerViewController = centerController;
}
Run Code Online (Sandbox Code Playgroud)

如果我误解了你的问题,那么我只是输入世界上最长的答案,徒劳无益.我希望这有帮助,让我知道是否有任何部分需要澄清!