ios 4.3 中的标签栏项目外观

Posted

技术标签:

【中文标题】ios 4.3 中的标签栏项目外观【英文标题】:Tabbar item appearance in ios 4.3 【发布时间】:2013-04-02 07:00:20 【问题描述】:

我有一个 UITabbar,我正在自定义它的外观,它在 ios >5 中运行良好。但是当我在 iphone 4.3 模拟器中运行相同的外观时,它会因为标签栏的外观而崩溃。

 for(UIViewController *tab in  tabBarController1.viewControllers)

        
            [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                    [UIFont fontWithName:@"Arial" size:16.0], UITextAttributeFont, nil]
                                          forState:UIControlStateNormal];
        

如何在 iphone 4.3 模拟器中也获得相同的外观?

任何帮助都会很明显......

【问题讨论】:

***.com/questions/2576592/… setTitleTextAttributes 在 ios 4.3 中不可用,这就是它崩溃的原因。你必须正确地使用你自己的 tababrcontroller @AmanAggarwal 在编写我自己的 tabbarcontroller 时我正在 appdelegate 中编写它。你能告诉我如何编写自己的 tabbarcontroller 吗? 看看这个cocoacontrols.com/… 【参考方案1】:

@Aman Aggarwal 给出了答案

苹果文档说

setTitleTextAttributes:forState:为给定的控件状态设置标题的文本属性。 ... 可用性适用于 iOS 5.0 及更高版本。

仅适用于 iOS 5.0 及更高版本

【讨论】:

好的,我发现它在 ios 5 或更高版本中可用。但是可能有一些替代方法可以在 4.3 中实现相同的外观但不同的属性。我想知道吗? 我认为您会找到解决方法,但如果您需要在应用商店上发布,我会注意它会通过提交过程。【参考方案2】:

它需要一个棘手的实现。这是一个。您可以在应用程序的标签栏上添加按钮。它很棘手,但很有效。

为此,您必须继承 UITabBarController

类似的东西

BaseViewController.h

@interface BaseViewController : UITabBarController



// Create custom UIButtons and add it to the tab bar
-(void) addButtons;

@end

BaseViewController.m

#import "BaseViewController.h"
#import "AppDelegate.h"

@implementation BaseViewController
// Create custom UIButtons and add it to the tab bar

-(void) addButtons

    UIImageView *backgroundImage    =   [[UIImageView alloc] initWithFrame: CGRectMake(20, 0, 330, 60)];
    [backgroundImage setImage: [UIImage imageNamed: @"bottom.png"]];
    [backgroundImage setCenter: self.tabBar.center];
    [self.view addSubview: backgroundImage];
    [backgroundImage release];

    UIButton    *firstTabbarButton  =   [UIButton buttonWithType: UIButtonTypeCustom];
    firstTabbarButton.frame = CGRectMake(20.0, 52.0, 68, 49);
    [firstTabbarButton setBackgroundImage:[UIImage imageNamed: @"home_normal.png"] forState:UIControlStateNormal];
    [firstTabbarButton setBackgroundImage:[UIImage imageNamed: @"home_normal.png"] forState:UIControlStateHighlighted];
    [firstTabbarButton setBackgroundImage:[UIImage imageNamed: @"home.png"] forState:UIControlStateSelected];
    [firstTabbarButton addTarget: self action: @selector(goButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
    [firstTabbarButton setCenter: CGPointMake(33, self.tabBar.center.y)];
    [firstTabbarButton setTag: 1000];
    [firstTabbarButton setAdjustsImageWhenHighlighted: NO];
    [self.view addSubview: firstTabbarButton];

    UIButton    *secondTabbarButton  =   [UIButton buttonWithType: UIButtonTypeCustom];
    secondTabbarButton.frame = CGRectMake(20.0, 52.0, 68, 49);
    [secondTabbarButton setBackgroundImage:[UIImage imageNamed: @"lab_normal.png"] forState:UIControlStateNormal];
    [secondTabbarButton setBackgroundImage:[UIImage imageNamed: @"lab_normal.png"] forState:UIControlStateHighlighted];
    [secondTabbarButton setBackgroundImage:[UIImage imageNamed: @"lab_hover.png"] forState:UIControlStateSelected];
    [secondTabbarButton addTarget: self action: @selector(goButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
    [secondTabbarButton setCenter: CGPointMake(96, self.tabBar.center.y)];
    [secondTabbarButton setTag: 1001];
    [secondTabbarButton setAdjustsImageWhenHighlighted: NO];
    [self.view addSubview: secondTabbarButton];

    UIButton    *thirdTabbarButton  =   [UIButton buttonWithType: UIButtonTypeCustom];
    thirdTabbarButton.frame = CGRectMake(20.0, 52.0, 68, 49);
    [thirdTabbarButton setBackgroundImage:[UIImage imageNamed: @"others.png"] forState:UIControlStateNormal];
    [thirdTabbarButton setBackgroundImage:[UIImage imageNamed: @"others.png"] forState:UIControlStateHighlighted];
    [thirdTabbarButton setBackgroundImage:[UIImage imageNamed: @"others_hover.png"] forState:UIControlStateSelected];
    [thirdTabbarButton addTarget: self action: @selector(goButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
    [thirdTabbarButton setCenter: CGPointMake(287, self.tabBar.center.y)];
    [thirdTabbarButton setTag: 1004];
    [thirdTabbarButton setAdjustsImageWhenHighlighted: NO];
    [self.view addSubview: thirdTabbarButton];

    UIButton    *forthTabbarButton  =   [UIButton buttonWithType: UIButtonTypeCustom];
    forthTabbarButton.frame = CGRectMake(20.0, 52.0, 68, 49);
    [forthTabbarButton setBackgroundImage:[UIImage imageNamed: @"history_normal.png"] forState:UIControlStateNormal];
    [forthTabbarButton setBackgroundImage:[UIImage imageNamed: @"history_normal.png"] forState:UIControlStateHighlighted];
    [forthTabbarButton setBackgroundImage:[UIImage imageNamed: @"history.png"] forState:UIControlStateSelected];
    [forthTabbarButton addTarget: self action: @selector(goButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
    [forthTabbarButton setCenter: CGPointMake(223, self.tabBar.center.y)];
    [forthTabbarButton setAdjustsImageWhenHighlighted: NO];
    [forthTabbarButton setTag: 1003];
    [self.view addSubview: forthTabbarButton];

    UIImage *centerImage  =   [UIImage imageNamed:@"go_btn.png"];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 52.0, 62, 62);
    [button setTag: 1002];
    [button setBackgroundImage:centerImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateSelected];
    [button addTarget: self action: @selector(goButtonClicked:) forControlEvents: UIControlEventTouchUpInside];

    CGFloat heightDifference = button.frame.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];


@end

这会奏效。但请注意应用商店提交指南。试试这个。

【讨论】:

以上是关于ios 4.3 中的标签栏项目外观的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin iOS:替换当前标签栏项目中的视图控制器

iOS 自定义标签栏项目比标签栏高

在 iOS 中以编程方式在标签栏上添加图像

iOS 13 大型导航栏外观 + Tabbar 图像

标签栏在 iOS 中不可见

IOS从标签栏项目执行segue