9

如下图所示,Twitter 为每个推送的视图控制器使用不同的导航栏颜色。

我几乎尝试了所有方法(setbackgroundimage、backgroundcolor、bartintcolor 等),但似乎没有任何效果。我认为 Twitter 使用自定义转换来模拟推送,因为在我看来,每个视图控制器都有自己的导航栏和自己的颜色。

在此处输入图像描述在此处输入图像描述

4

4 回答 4

4

If you want to handle navigationBar with different barTintColors, Code School had a tutorial about it. (iOS App: Creating a Custom Nav Bar) It could also extended to different backgrounds by using setBackgroundImage:forBarMetrics: method.

There are following four steps:

  1. Handle this in viewWillAppear of the source view controller, hide the navigationBar that navigationController provided and create a new navigationBar to the superView.

    - (void)styleNavBar
    {
        // 1. hide the existing nav bar
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    
        // 2. create a new nav bar and style it
        UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
        [newNavBar setTintColor:[UIColor whiteColor]];
    
        // 3. add a new navigation item w/title to the new nav bar
        UINavigationItem *newItem = [[UINavigationItem alloc] init];
        newItem.title = @"Source";
        [newNavBar setItems:@[newItem]];
    
        // 4. add the nav bar to the main view
        [self.view addSubview:newNavBar];
    }
    
  2. Do the same trick in viewWillAppear of the destination view controller, and create a backBarButtonItem as new navigationBar'sleftBarButtonItem.

    - (void)styleNavBar 
    {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
        [newNavBar setTintColor:[UIColor blueColor]];
        UINavigationItem *newItem = [[UINavigationItem alloc] init];
        newItem.title = @"Destination";
    
        // BackButtonBlack is an image we created and added to the app’s asset catalog
        UIImage *backButtonImage = [UIImage imageNamed:@"BackButtonBlack"];
    
        // any buttons in a navigation bar are UIBarButtonItems, not just regular UIButtons. backTapped: is the method we’ll call when this button is tapped
        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(backTapped:)];
    
        // the bar button item is actually set on the navigation item, not the navigation bar itself.
        newItem.leftBarButtonItem = backBarButtonItem;
    
        [newNavBar setItems:@[newItem]];
        [self.view addSubview:newNavBar];
    }
    
  3. Fill out the backTapped: method so that user is able to tap-to-popover from destination view controller.

    - (void)backTapped:(id)sender
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
  4. Considering the swipe-to-pop situation, setting the gesture recognizer’s delegate to self in viewWillAppear of the destination view controller. (The author: The solution here is a bit of a hack.)

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self styleNavBar];
    
        __weak id weakSelf = self;
        self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
    }
    
于 2015-01-03T00:27:43.113 回答
3

这对于 swift3 :

  1. 您可以使用空图像设置原始导航栏背景:

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    
  2. 这样,视图层次结构中的所有其他 VC 都将采用透明的导航栏。

  3. 在另一个 VC 中,如果您想设置自定义图像或自定义颜色,只需将背景视图放在导航栏的位置,这在视图中确实加载了特定视图控制器的方法。

    let viewNavBar = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 64))
    viewNavBar.backgroundColor = UIColor.white
    view.addSubview(viewNavBar)
    

有了它,您可以在 viewNavBar 内设置任何背景或图像,并且不要弄乱导航栏的整体配置。

于 2017-02-11T16:54:22.917 回答
2

Twitter 不会改变导航栏的颜色。当您查看用户的个人资料时,它是用户封面照片的模糊版本。

正如您在转换中看到的那样,整个用户配置文件视图替换了之前的视图。导航栏没有改变,它被替换了。他们甚至可能不使用 UINavigationBar(或者至少不使用导航控制器中的那个)。

“栏”是一个自定义视图,显示用户的封面照片,后退/搜索/推文按钮出现在他们通常的位置。当您向下滚动时,用户的封面照片会缩小、模糊并附加到屏幕顶部——此时,它看起来就像一个普通的导航栏。此时,用户的姓名和推文计数也会向上滚动到导航栏的中心。

这很有趣,他们对用户个人资料的整个视图结构可能使用了很多技巧。但模仿他们的个人资料视图并不是一项简单的任务,他们所做的不仅仅是改变导航栏的色调。如果您只是想这样做,Undo 的回答效果很好。但是,您可能还必须在您的viewWillAppear方法(旧视图和新视图)中重置色调颜色。

于 2014-09-30T23:43:17.867 回答
0

尝试查找此 GitHub 存储库,我认为它可以帮助您实现该效果https://github.com/kingiol/KDInteractiveNavigationController

于 2016-04-21T14:55:39.393 回答