为标签栏设置背景图像

2024-01-06

我正在尝试以编程方式设置应用程序中选项卡栏的背景图像。我的代码如下:

根视图控制器.h

IBOutlet UITabBar *mainTabBar;
    IBOutlet UITabBarItem *settingsBarItem;
    IBOutlet UITabBarItem *infoBarItem;
    IBOutlet UITabBarItem *aboutBarItem;

根视图控制器.m

-(void)viewDidLoad {

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smallMenuBackground.png"]];    
    [mainTabBar insertSubview:imageView atIndex:0];
    [imageView release];

    [super viewDidLoad];
}

这对我不起作用。

UPDATE

2012 年 1 月 23 日更新

好吧,我已经取得了一些进展。自从我升级到 Xcode 4.2 和 IOS5 后,这个问题就停止了。我设法使用 Interface Builder 中的选项将其恢复,但现在它仅适用于 IOS5。理想情况下,我希望以编程方式工作,但现在我会选择 IB 解决方案。

我似乎无法让它适用于任何以前的版本。

注意:我的 TabBar 仅位于我的 RootViewController 上,这是我的应用程序的主屏幕。

理想情况下,如果我能让 Nithin 建议的代码正常工作,那就太好了:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBG.png"]];

if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
    //iOS 5
    [self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
    //iOS 4.whatever and below
    [self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}

[imageView release];

任何帮助,将不胜感激。

问候, 斯蒂芬


您可以使用 UITabBarController 的自定义类并覆盖您的 tabBarController。您可以在那里设置所需的按钮及其带有图像的操作。

您的自定义选项卡栏控制器类如下所示:

// 自定义TabBarController.h

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {
    UIButton *settingsButton;
    UIButton *infoButton;
    UIButton *aboutUsButton;
}

@property (nonatomic, retain) UIButton *settingsButton;
@property (nonatomic, retain) UIButton *infoButton;
@property (nonatomic, retain) UIButton *aboutUsButton;

-(void) addCustomElements;
-(void) selectTab:(int)tabID;

@end

// 自定义TabBarController.m

#import "CustomTabBarController.h"

@implementation CustomTabBarController

@synthesize settingsButton, infoButton, aboutUsButton;

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


}
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self addCustomElements];
}

-(void)addCustomElements
{
    // Background
    UIImageView* bgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBarBackground.png"]] autorelease];
    bgView.frame = CGRectMake(0, 420, 320, 60);
    [self.view addSubview:bgView];

    // Initialise our two images
    UIImage *btnImage = [UIImage imageNamed:@"settings.png"];
    UIImage *btnImageSelected = [UIImage imageNamed:@"settingsSelected.png"];

    self.settingsButton = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
    settingsButton.frame = CGRectMake(10, 426, 100, 54); // Set the frame (size and position) of the button)
    [settingsButton setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
    [settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted]; // Set the image for the selected state of the button
    [settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
    [settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateDisabled];
    [settingsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];
    [settingsButton setTag:101]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
    [settingsButton setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

    // Now we repeat the process for the other buttons
    btnImage = [UIImage imageNamed:@"info.png"];
    btnImageSelected = [UIImage imageNamed:@"infoSelected.png"];
    self.infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    infoButton.frame = CGRectMake(110, 426, 100, 54);
    [infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [infoButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [infoButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
    [infoButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

    [infoButton setTag:102];

    btnImage = [UIImage imageNamed:@"aboutUs.png"];
    btnImageSelected = [UIImage imageNamed:@"aboutUsSelected.png"];
    self.aboutUsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aboutUsButton.frame = CGRectMake(210, 426, 100, 54);
    [aboutUsButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
    [aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
    [aboutUsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];

    [aboutUsButton setTag:103];

    // Add my new buttons to the view
    [self.view addSubview:settingsButton];
    [self.view addSubview:infoButton];
    [self.view addSubview:aboutUsButton];

    // Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
    [settingsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [infoButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [aboutUsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked:(id)sender
{
    int tagNum = [sender tag];
    [self selectTab:tagNum];
}

- (void)selectTab:(int)tabID
{
    switch(tabID)
    {
        case 101:
            [settingsButton setSelected:true];
            [infoButton setSelected:false];
            [aboutUsButton setSelected:false];
            break;
        case 102:
            [settingsButton setSelected:false];
            [infoButton setSelected:true];
            [aboutUsButton setSelected:false];
            break;
        case 103:
            [settingsButton setSelected:false];
            [infoButton setSelected:false];
            [aboutUsButton setSelected:true];
            break;
    }   
    self.selectedIndex = tabID;
}

- (void)dealloc {
    [settingsButton release];
    [infoButton release];
    [aboutUsButton release];

    [super dealloc];
}

@end

希望这会对您有很大帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为标签栏设置背景图像 的相关文章

  • iPhone VS ipad开发流程(异同)

    我是 iOS 开发新手 我正在努力弄清楚这些事情 iPhone 和 iPad 上的项目 两者有哪些相同之处 编码 图形或 UI 应用程序的设计模式相同还是不同 等待答复 谢谢 就我个人而言 我发现 iPhone 和 iPad 之间唯一真正的
  • 设置捆绑包中组的标题页脚

    How can I leave a message under group type in Setting bundle something like this 它记录在这里 https developer apple com librar
  • 当导航栏半透明 = false 时,UISearchBar 超出屏幕范围

    我尝试将 UISearchBarController 添加到 tableView 但是当我设置 UINavigationBar appearance translucent false 时 UISearchBar 隐藏在屏幕之外 在 Tab
  • 第一次推送 vc 然后呈现模态 vc 12 次时如何弹出?

    我有一个导航控制器 我首先推了一个 VC 然后提出了 12 个模态 VC 现在我想弹出到根视图控制器 我怎样才能做到这一点 请帮帮我 您需要忽略已呈现的 12 个模态视图 popViewController 或 popToRootViewC
  • 从每个 UIWebView 请求中获取 http 响应代码

    我需要在加载 webview fo 中的任何 url 时检查响应状态代码 现在 我们可以考虑我在 Web 视图中加载的任何 Web 应用程序 因此 我需要跟踪该 Web 视图中的每个请求并相应地检查响应代码 为了查找响应代码 我需要在 ui
  • iPhone 版 Twitter 书签如何工作?

    Twitter 客户端 以前称为 Tweetie 允许您在 Safari 中定义启动应用程序的书签 我想知道哪个 iPhone API 允许您注册协议说明符 或任何名称 在本例中为 tweetie 以便此小书签正常工作 可以找到说明here
  • 带有徽章编号的 UISegmentedControl

    就像标签栏一样 我想在其上显示徽章UISegmentedControl 因为我看不到任何预定义的方法UISegmentedControl就像可用于UITabBar 我考虑过将徽章作为图像添加到其顶部 但也许有更好的方法 Here https
  • 以编程方式从 iPhone 向 WordPress 博客发表评论

    我在本地主机服务器上安装了 WordPress 博客 还制作了一个 iPhone 应用程序来通过 rss 浏览博客 我尝试使用此代码以编程方式发布评论 define post url http localhost web wp wp com
  • iPhone/iPad 应用程序代码混淆 - 有可能吗?值得? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我已经研究了很多 无论是在 SO 上 还是到处谷歌搜索 但我似乎找不到关于用 Objective C 编写的 iPhone iPad
  • 将 UIWebView 标题发送到 UINavigationBar

    我正在尝试将 UIWebView 页面标题发送到 UINavigationBar 如果用户单击链接 UINavigationBar 显示后退按钮 但如果在主页上隐藏后退按钮 我也希望如此 要将标题页检索到 UIWebView 中 您可以使用
  • 在 ARC 中异步生成 CGImagesForTimes

    如果我在启用了 ARC 的项目中运行以下命令 则完成处理程序永远不会触发 但如果没有 ARC 它会按预期工作 我在这里缺少什么 NSURL url NSURL URLWithString http media w3 org 2010 05
  • 外围 BLE 设备的唯一标识符

    所以我有外围设备BLE设备 我需要一些标识符以便稍后与另一部 iPhone 共享 我连接的示例iPhone A 为外围设备 iPhone A 将外围设备的标识符保存到数据库中 稍后我可以轻松获取iPhone B 并连接到通过该标识符找到的外
  • 带有内容矩形抖动的核心动画

    在我的 益智 游戏中 这些碎片是使用CALayer对于每件作品 有 48 块 在 8x6 网格中 每块为 48x48 像素 我不确定这是否是太多层 但如果这不是最好的解决方案 我不知道什么是 因为使用 Quartz2D 每帧重新绘制整个显示
  • 为 iPhone 创建 .ipa

    我为 iPhone 开发了一款应用程序 构建后 我在构建文件夹中得到了 app 文件 我的应用程序名称是Myapp 然后我在build文件夹中得到了Myapp app文件 我的问题是我想创建 ipa 文件 这是怎么回事 它是为了安装 越狱的
  • 从 xib 中提取 UI 项目?

    经过一番浏览文档后 我决定使用以下技术 使用 viewWithTag 从 loadNibNamed owner options 返回的主根视图中提取子视图 首先 我有点困惑 因为我假设 loadNibNamed owner options
  • 从 NSString 的第一行删除换行符

    我怎样才能删除第一个 nNSString 中的字符 编辑 只是为了澄清一下 我想做的是 如果字符串的第一行包含 n 字符 请将其删除 否则不执行任何操作 即 如果字符串是这样的 nhello this is the first line n
  • 相机叠加图片

    edit 3 好消息和坏消息 好消息是 在连接检查器中 通过断开覆盖 UIToolbar 并连接 UIImageview 我看到theKing 但是 坏消息 我没有看到我也需要的 UIToolbar 所以现在的问题是 当用户完成这里操作后
  • 未安装的应用程序的URL方案

    简单的问题 我正在开发一个将注册自己的 URL 方案的应用程序 我计划通过人们最喜欢的 QRCode 阅读器使用 QRCode 启动该应用程序 我的问题 如果我的应用程序尚未安装在他们的 iPhone iPad 上 会发生什么 他们会被引导
  • 无法下载应用程序 - 此时无法下载“APP”

    我的应用程序有 PLUS 版本和常规版本 我使用不同的目标对它们进行存档 我将 ipa 上传到 TestFlight 也上传到我的曲棍球服务器 PLUS 版本总是下载得很好 但普通版本总是给我 无法下载应用程序 错误 我根本没有更改两个版本
  • iPhone / iPad IOS 应用程序仪器内存计数与 task_info 内存计数

    我一直在使用 Instruments Leak Tester 它给出了大约 1 3 meg 的应用程序总分配数字 但是 当使用 task info 时 它会报告更大的内存量 例如 10 20 meg 我想我只是想确认task info正在返

随机推荐

  • 具有事件处理程序的 asp.net 动态按钮

    我在 ASP NET 中动态生成的按钮及其事件处理程序遇到了一个小问题 我为特殊用户生成了一个带有附加按钮的灵活表格 这些按钮将动态生成 效果很好 但我无法让事件处理程序工作 以下是我的代码中的一些片段 构建按钮 在自己的函数中 Butto
  • 使用 JavaScript 从 C# 执行 doPostBack

    您好 我有一个父页面 它打开一个弹出窗口 用户在子弹出页面上进行一些更改 然后单击保存按钮 当用户单击保存按钮时 我想PostBack到父页面 以便在弹出窗口中所做的更改可以在父窗口中看到 Question 如何实现上述场景 我想在aspx
  • 将单个 doc 文件转换为 pdf

    我正在使用以下代码如何以编程方式将 Word 文件转换为 PDF https stackoverflow com questions 607669 how do i convert word files to pdf programmati
  • 在 virtualenv 中创建项目时没有名为 django.core 的模块

    所以我环顾了很多与我类似的问题 但我找不到具体的答案 我的电脑规格是 Windows 7 64 位 我的问题是这样的 1 我使用 pip 安装了 virtualenv pip install virtualenv 2 之后我创建并激活了一个
  • JUnit 5 有类似 Cucumber 的标记钩子的东西吗?

    有没有办法像 Cucumber 一样在 JUnit5 中用钩子注释单个测试 例如 在黄瓜中 可以编写像这样的钩子 Before SomeTest public void beforeSomeTest 然后 如果我们使用 SomeTest 注
  • 如何从 calibrateCamera 结果获取相机世界位置?

    我正在使用 calibrateCamera 执行相机校准 输出的一部分是一组 Rodrigues 旋转向量和 3 D 平移向量 我对摄像机的世界位置感兴趣 如果我直接绘制平移点 结果看起来不正确 我觉得我的坐标空间很混乱 但我在解析 ope
  • nginx try_files 和 add_header

    有人可以解释一下吗 我有一个 nginx 服务器块 其中包含以下代码片段 location try files uri uri index html 基本上 我用它来提供 Angular SPA 它运作良好且很棒 现在我想追加Access
  • 显示韩语字符 - iOS 应用程序

    我正在尝试在我的 iPhone 应用程序中显示韩文文本 该应用程序将字母的 Unicode 逐一附加到 NSMutableString 中 并在附加每个字母后在屏幕上显示该字符串 我了解连接字母 Jamo 有一些规则 是否有一个函数可以自动
  • 从函数内部禁用 `functools.lru_cache`

    我想要一个可以使用的功能functools lru cache 但不是默认情况下 我正在寻找一种使用函数参数的方法 该函数参数可用于禁用lru cache 目前 我有该函数的两个版本 其中一个版本带有lru cache和一个没有 然后我有另
  • 导入错误:无法导入名称应用程序

    我正在尝试按照教程进行操作 But from kivy import App gives ImportError cannot import name App 我怎样才能解决这个问题 我已经安装了kivy 1 8 0和cython 0 20
  • c# - 如果方法运行时间太长,则中止方法的执行

    如果运行时间太长 如何中止方法的执行 e g string foo DoSomethingComplex but if DoSomethingComplex 花费的时间太长 比如说 20 秒 然后只需将 foo 设置为 您可以创建一个运行您
  • Spring MVC:表单标签和命令错误

    我正在使用 Spring 框架进行编程 在处理表单标记中的 jsp 第 2 行 时显示以下错误后 我感到绝望 Error ERROR org springframework web servlet tags form InputTag Ne
  • Now.sh 构建中断的原因是:当前未启用对实验性语法“decorators-legacy”的支持

    预期的 添加后decko 对装饰器的支持 以及添加对experimetalDecoractors in my tsconfig js并使用 babel plugin proposal decorators in 包 json My now
  • 有没有办法在 Svelte 中将 props 声明为可选

    我创建了一些带有可选道具的组件 例如hide true 我的问题是 当我不通过该道具时 这些恼人的错误消息总是充斥着我的控制台
  • 点云生成

    我有一个 3D 几何形状 必须将其转换为点云 所得到的点云可以被认为等同于对象的激光扫描输出的点云 不需要生成网格 生成的点可能是均匀分布的 也可能只是随机分布的 没关系 3D形状可以以3D数学公式的形式提供 这必须使用 MATLAB 来完
  • 从命令行指定 dockerignore

    我有一个 dockerignore 文件 但对于一个用例 我想在命令行指定 dockerignore 的内容 例如 docker build ignore node modules t foo 有没有办法从命令行执行此操作 我在文档中没有看
  • ECMAScript 6:WeakSet 的用途是什么?

    WeakSet 应该通过弱引用来存储元素 也就是说 如果一个对象没有被其他任何东西引用 那么它应该从 WeakSet 中清除 我写了以下测试 var weakset new WeakSet numbers 1 2 3 weakset add
  • 更改 HTML 元素的背景颜色

    我有一张包含 100 多个不同大小和尺寸的几何形状的图像 我在它上面使用了图像映射并为每个分配了 ID area like area 我在 MySQL 数据库中存储了有关每个形状的记录 例如 box id color code 1 AEEE
  • 优化 QtCreator 编译器的配置

    我在 Windows 7 中使用 QtCreator 我想将其配置为使用第三级优化 O3 用于 C 编译器 我怎样才能加快我的代码速度以及需要进行哪些更改 尝试将下一行添加到您的 pro 文件中 remove possible other
  • 为标签栏设置背景图像

    我正在尝试以编程方式设置应用程序中选项卡栏的背景图像 我的代码如下 根视图控制器 h IBOutlet UITabBar mainTabBar IBOutlet UITabBarItem settingsBarItem IBOutlet U