Search code examples
iosxcodeinterface-builderuitabbarcontrolleruitabbar

add button over tabbarcontroller


Is it possible to add a button over a tabbar controller using interface builder?

I am trying to do this in interface builder but every time I add the button over tabbar it fills up the rest of the screen with the button instead of just putting the button over the tabbar.

Something like the camera in instagram.


Solution

  • this is how i solved the problem:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIImage *buttonImage = [UIImage imageNamed:@"cameraTabBarItem.png"];
        UIImage *highlightImage = [UIImage imageNamed:@"cameraTabBarItem.png"];
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
        [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
        CGFloat heightDifference = buttonImage.size.height - self.tabBarController.tabBar.frame.size.height;
        if (heightDifference < 0)
            button.center = self.tabBarController.tabBar.center;
        else
        {
            CGPoint center = self.tabBarController.tabBar.center;
            center.y = center.y - heightDifference/2.0;
            button.center = center;
        }
        [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarController.view addSubview:button];
    }