繁体   English   中英

通用iOS应用中的自定义TabBar

[英]Custom TabBar in universal iOS 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!

您将必须继承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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM