Question

I am trying to create a picker type thing when one button on a UITabBar is pressed. If you look at tumblrs iphone app, you will see what I mean. I assume they are presenting a UIView over whatever viewcontroller the user is currently on. How would I do this?

enter image description here

Was it helpful?

Solution

This question to me is more like a Custom Tab bar button question, i think what tumblr did is add a custom button in the middle of the tab bar, and present a modal view controller when click that button.

i did a very quick test on my test project, some number are hard coded for testing purpose, you may have to tweak that a little bit.

  1. Add a custom button in the middle of your tab bar. (add following code to the viewWillAppear of the first view controller)

    - (void)viewWillAppear:(BOOL)animated
    {
        UIImage *buttonImage = [UIImage imageNamed:@"classic-batman-logo.jpg"];
        UIImage *highlightImage = [UIImage imageNamed:@"classic-batman-logo.jpg"];
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0, 0.0, 60, 50);
        [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
        button.center = CGPointMake(self.tabBarController.tabBar.center.x , 30);
        [button addTarget:self action:@selector(centerButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarController.tabBar addSubview:button];
    }
    
  2. then simply present your target view controller in the centerButtonClicked function

    - (void)centerButtonClicked
    {
       UIViewController *vc = [[UIViewController alloc]init];
       vc.view.backgroundColor = [UIColor redColor];
       [self presentViewController:vc animated:YES completion:nil];
    }
    

This is just a 10 minutes test i did, should look like this:

enter image description here

Maybe there are a couple of things you want to pay attention:

  • change the hard coded center point when you implement your own code,

  • and even better, you can create a custom tabBar with that center button, and set up a delegate method for that button click function. So every time when you click that button from different view controller, it will have different behavior.

Hope that gives you some rough idea about how to achieve this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top