UITabBar Wierd Height Difference

时间:2015-06-14 18:52:28

标签: ios objective-c xcode ios8 uitabbarcontroller

enter image description here

我想要以编程方式添加条形图时,在Storyboard图像中看到默认高度。知道如何获得它吗?以下是我用来向我的应用添加UITabBarController的代码。

- (void)setCustomTabBar
{
    //TODO: Fix TabBar height
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    TimelineVC *tvc = [[TimelineVC alloc] initWithNibName:@"TimelineVC" bundle:nil];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tvc];

    TimelineVC *tvc2 = [[TimelineVC alloc] initWithNibName:@"TimelineVC" bundle:nil];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tvc2];
    tvc2.showFriends = true;

    NotificationVC *nvc = [[NotificationVC alloc] initWithNibName:@"NotificationVC" bundle:nil];
    nav3 = [[UINavigationController alloc] initWithRootViewController:nvc];

    ProfileVC *pvc = [[ProfileVC alloc] initWithNibName:@"ProfileVC" bundle:nil];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:pvc];

    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    [[UITabBar appearance] setShadowImage:nil];
    [tabBarController setViewControllers:@[nav1, nav2, nav3, nav4]];
    tabBarController.selectedIndex = 0;


    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat tabBarWidth = screenRect.size.width/4;

    [[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor darkGrayColor] forSize:CGSizeMake(screenRect.size.width, 49) withCornerRadius:0]];
    [[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:26/255.0 green:163/255.0 blue:133/255.0 alpha:1] forSize:CGSizeMake(tabBarWidth, 49) withCornerRadius:0]];

    tabBarController.tabBar.translucent = NO;

    UIImage *normalImage, *selectedImages;
    normalImage =[UIImage imageNamed:@"TimeLineIcon"];
    selectedImages = [UIImage imageNamed:@"TimeLineIcon"];

    UIImage *normalImage2, *selectedImages2;
    normalImage2 =[UIImage imageNamed:@"FriendsIcon"];
    selectedImages2 = [UIImage imageNamed:@"FriendsIcon"];

    UIImage *normalImage3, *selectedImages3;
    normalImage3 =[UIImage imageNamed:@"NotificationIcon"];
    selectedImages3 = [UIImage imageNamed:@"NotificationIcon"];

    UIImage *normalImage4, *selectedImages4;
    normalImage4 =[UIImage imageNamed:@"ProfileIcon"];
    selectedImages4 = [UIImage imageNamed:@"ProfileIcon"];


    //TODO: FOR LOOP
    nav1.tabBarItem.selectedImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav1.tabBarItem.image = [selectedImages imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav1.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

    nav2.tabBarItem.selectedImage = [normalImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav2.tabBarItem.image = [selectedImages2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav2.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

    nav3.tabBarItem.selectedImage = [normalImage3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav3.tabBarItem.image = [selectedImages3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav3.tabBarItem.badgeValue = @"1";
    nav3.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

    nav4.tabBarItem.selectedImage = [normalImage4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav4.tabBarItem.image = [selectedImages4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav4.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

    self.window.rootViewController = tabBarController;
    [self.window addSubview:tabBarController.view];

    [self enableSupportKit];
}

2 个答案:

答案 0 :(得分:0)

[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];

这对我有帮助。 希望它有所帮助。

答案 1 :(得分:0)

检查你的launchImage.for例如,它在你的模拟器中是正确的,但如果你使用你的iphone(我猜是iphone6)。删除iphone6 launchimage,然后你会发现问题。