通用iOS应用程序中的自定义TabBar

时间:2014-02-04 22:10:33

标签: ios uitabbar

我希望重新创建凸起的标签栏,如许多应用程序中所示,但我希望在通用应用程序中执行此操作,其中需要更改凸起的位置。

目前我没有太多运气创造这个,所以来到这里进行启蒙,如果不这样做,我可能只是创建一个自定义标签栏背景。

如果是这种情况,是否可以在app委托中使用以区分这两个设备?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:@"iPadtabBar.png"]];

}
else {

UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:@"iPhonetabBar.png"]];

}

你可能在想,为什么他不只是测试它...那会更容易,如果我可以,我宁愿先问,但我不会让我的Mac回到下周开始!< / p>

1 个答案:

答案 0 :(得分:0)

您必须继承UITabBarController并自定义该中心选项卡按钮。这里有一个很好的例子:https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar/Classes

头文件:     @interface CustomTabBarViewController:UITabBarController {     }

// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage;

@end

实施档案:

@implementation CustomTabBarViewController

// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
    UIViewController* viewController = [[UIViewController alloc] init];
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
    return viewController;
}

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    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];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end
相关问题