Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way of adding a custom centre button to a tab bar?

Many apps have a standard tab bar with a custom button in the centre which performs some special function. Some examples can be found here:

http://mobile-patterns.com/custom-tab-navigation

What's the best way of implementing this?

like image 961
cannyboy Avatar asked Dec 01 '22 01:12

cannyboy


1 Answers

Here you can see how to implement that button. I am gonna paste the code so it stays here forever:

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.tabBar.frame.size.height;
if (heightDifference < 0)
    button.center = self.tabBar.center;
else
{
    CGPoint center = self.tabBar.center;
    center.y = center.y - heightDifference/2.0;
    button.center = center;
}

[self.view addSubview:button];

Hope it helps

like image 120
Fran Sevillano Avatar answered Dec 03 '22 14:12

Fran Sevillano