如何更改 iOS 7 中未选中的选项卡栏项目的颜色?

2024-01-28

我试图更改选项卡栏中未选定图像的颜色,默认情况下它们会更改为灰色,即使图像是另一种颜色。

我已经搜索过这个,但答案仅适用于 iOS 6 或更低版本。


最近这让我很生气,所以我编写了自己的类来处理它,它适用于我尝试过的每个版本的 iOS;)它真的很容易扩展来做任何你想做的事情!

GozTabBar.h:

#import <UIKit/UIKit.h>
#import "GozTabBarItem.h"

@protocol GozTabBarDelegate;

@interface GozTabBar : UIView
{
    UITapGestureRecognizer* pTapGestureRecognizer;

}

@property                                   UIColor*                    backgroundColour;
@property (unsafe_unretained, nonatomic)    id < GozTabBarDelegate >    delegate;
@end

@protocol GozTabBarDelegate < NSObject >

- (int)             getNumberOfTabBarItemsForTabBar: (GozTabBar*) pTabBar;
- (GozTabBarItem*)  getTabBarItemsAtIndex: (int) index ForTabBar: (GozTabBar*) pTabBar;
- (void)            selectedItemAtIndex: (int) index ForTabBar: (GozTabBar*) pTabBar;
- (int)             getSelectedItemIndexForTabBar: (GozTabBar*) pTabBar;
@end

GozTabBar.m

#import "GozTabBar.h"
#import "GozTabBarItem.h"

@implementation GozTabBar

@synthesize backgroundColour;
@synthesize delegate;

const int leftEdgeInset         = 8;
const int rightEdgeInset        = 8;

const int topEdgeInset          = 8;
const int bottomEdgeInset       = 8;

- (id)init
{
    self = [super init];
    if (self)
    {
        pTapGestureRecognizer   = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
        [self addGestureRecognizer: pTapGestureRecognizer];
    }
    return self;
}

- (id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        pTapGestureRecognizer   = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
        [self addGestureRecognizer: pTapGestureRecognizer];
    }
    return  self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        pTapGestureRecognizer   = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
        [self addGestureRecognizer: pTapGestureRecognizer];
    }
    return self;
}

// Recognise a tap on the item (If it is on an item).
- (void)onTap: (UIGestureRecognizer *)gestureRecognizer
{
    const int leftRightEdgeInset    = leftEdgeInset + rightEdgeInset;
    const int topBottomEdgeInset    = topEdgeInset + bottomEdgeInset;

    int numItems        = 0;
    if ( [self.delegate respondsToSelector: @selector( getNumberOfTabBarItemsForTabBar: )] )
    {
        numItems    = [self.delegate getNumberOfTabBarItemsForTabBar: self];
    }
    if ( numItems > 0 )
    {
        int widthLessInset  = (self.frame.size.width - leftRightEdgeInset);
        int itemWidth       = widthLessInset / numItems;

        int heightLessInset = self.frame.size.height - topBottomEdgeInset;
        int itemHeight      = heightLessInset;

        CGPoint tapPoint    = [gestureRecognizer locationInView: self];

        // Draw the custom items.
        for( int i = 0; i < numItems; i++ )
        {
            CGRect tabBarItemRect   = CGRectMake( leftEdgeInset + (itemWidth * i), 0, itemWidth, itemHeight );
            if ( CGRectContainsPoint( tabBarItemRect, tapPoint ) )
            {
                if ( [self.delegate respondsToSelector: @selector(selectedItemAtIndex:ForTabBar:)] )
                {
                    [self.delegate selectedItemAtIndex: i ForTabBar: self];
                }
                break;
            }
        }
    }
}


- (void)drawRect:(CGRect)rect
{
    const int leftRightEdgeInset    = leftEdgeInset + rightEdgeInset;
    const int topBottomEdgeInset    = topEdgeInset + bottomEdgeInset;

    CGContextRef ctx    = UIGraphicsGetCurrentContext();

    // Fill the background in the relevant colour.
    CGContextSetFillColorWithColor( ctx, backgroundColour.CGColor );
    CGContextFillRect( ctx, rect );

    int numItems        = 0;
    if ( [self.delegate respondsToSelector: @selector( getNumberOfTabBarItemsForTabBar: )] )
    {
        numItems    = [self.delegate getNumberOfTabBarItemsForTabBar: self];
    }
    if ( numItems > 0 )
    {
        int widthLessInset  = (rect.size.width - leftRightEdgeInset);
        int itemWidth       = widthLessInset / numItems;

        int heightLessInset = rect.size.height - topBottomEdgeInset;
        int itemHeight      = heightLessInset;

        int selectedIndex   = 0;
        if ( [self.delegate respondsToSelector: @selector(getSelectedItemIndexForTabBar:)] )
        {
            selectedIndex   = [self.delegate getSelectedItemIndexForTabBar: self];
        }


        // Draw the custom items.
        for( int i = 0; i < numItems; i++ )
        {
            //GozTabBarItem*    pItem   = [self.items objectAtIndex: i];
            GozTabBarItem*  pItem   = nil;
            if ( [self.delegate respondsToSelector: @selector(getTabBarItemsAtIndex:ForTabBar:)] )
            {
                pItem   = [self.delegate getTabBarItemsAtIndex: i ForTabBar: self];
            }
            if ( pItem != nil )
            {
                CGRect  tabBarItemRect      = CGRectMake( leftEdgeInset + (itemWidth * i), topEdgeInset, itemWidth, itemHeight );
                CGPoint tabBarItemCenter    = CGPointMake( tabBarItemRect.origin.x + (tabBarItemRect.size.width / 2), tabBarItemRect.origin.y + (tabBarItemRect.size.height / 2) );

                UIImage* pDrawImage = nil;
                if ( i == selectedIndex )
                {
                    pDrawImage = pItem.selectedImage;
                }
                else
                {
                    pDrawImage = pItem.unSelectedImage;
                }

                CGRect drawRect = CGRectMake( tabBarItemCenter.x - (pDrawImage.size.width / 2), tabBarItemCenter.y - (pDrawImage.size.height / 2), pDrawImage.size.width, pDrawImage.size.height );
                [pDrawImage drawInRect: drawRect];
            }
        }
    }
}

GozTabBarItem.h

#import <UIKit/UIKit.h>

@interface GozTabBarItem : NSObject
{
}

@property UIImage*  selectedImage;
@property UIImage*  unSelectedImage;

- (id) initWithSelectedImage: (UIImage*) selectedImage andUnselectedImage: (UIImage*) unSelectedImage;

@end

GozTabBarItem.m

#import "GozTabBarItem.h"

@implementation GozTabBarItem

@synthesize selectedImage;
@synthesize unSelectedImage;

- (id) initWithSelectedImage: (UIImage*) selectedImg andUnselectedImage: (UIImage*) unSelectedImg
{
    self = [super init];
    if (self)
    {
        self.selectedImage      = selectedImg;
        self.unSelectedImage    = unSelectedImg;
    }
    return self;
}
@end

希望有帮助。

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

如何更改 iOS 7 中未选中的选项卡栏项目的颜色? 的相关文章

  • 当方向改变时,视图控制器会为我做什么?

    一个简单的iphone程序 由项目模板View based Application生成 有几个按钮 我添加了以下代码 void showInfo UIView view NSLog view bounds 6 2f 6 2f 6 2f 6
  • 更改“返回”键盘按钮的文本

    如何将 返回 按钮的标准文本更改为其他内容 我希望它是 添加 不幸的是 您可以使用以下命令将 Return 更改为这些预定义标签之一 returnKeyType财产 返回 默认 Go Google Join Next Route Searc
  • UIButton 触摸事件落入底层视图

    我创建了一个小UIView其中包含两个UIButton是 视图响应UITapGesture事件 按钮应该响应TouchUpInside 但是当我点击按钮时 响应者是底层视图 并且点击手势选择器被触发 寻找意见或建议 您可以修改橙色视图中响应
  • AVAudioRecorder 标准化音量

    我有一个可以录制音频的应用程序 我想知道如何才能增加增益 有没有办法标准化音频或以某种方式放大它 谢谢 豪伊 看来我找到了解决方案 根据文档 AVAudioPlayer 音量可以在 0 0 到 1 0 之间 显然它可以设置为大于1 0的值
  • Xcode 4 .xib 创建 iPad 版本

    我有一台 iPhone xib 我想将其变成 iPad xib 在 Xcode 3 中 有一个 创建 iPad 版本 菜单选项 我如何在 Xcode 4 中执行此操作 我目前调整了 xib 的大小 但是当我打开模拟项目 导航栏等 时 它会将
  • 当将 contentMode 设置为 UIViewContentModeScaleAspectFit 时,如何设置 UIImageView 左对齐或右对齐?

    我想在使用时控制图像对齐UIViewContentModeScaleAspectFit in UIImageView 例如 我有两个UIImageView在上述的一个视图中 这两个UIImageView的内容模式是UIViewContent
  • iOS中拖动时如何修改UIVisualEffectView的模糊度?

    目前 我正在使用 UIVisualEffectView 对图像应用模糊 我有一个 UIScrollView 当我在 scrollViewDidScroll 方法中向下拉滚动视图时 我正在更改 UIVisualEffectView 的 alp
  • 多次显示admob插页式广告怎么办?

    我有一个小型游戏应用程序 它有一个故事板 里面创建了像开始菜单 游戏区域 分数这样的场景 我在其中添加了 admob 横幅视图和插页式广告 我的横幅视图工作正常 但我的插页式广告只能工作一次 我在 viewdidload 上加载插页式广告
  • iOS模拟器找不到SDK,可能需要重新安装SDK

    在这里 我遇到了另一个问题 今天正在开发一个 iOS 应用程序 当我运行 iPhone 5 0 模拟器的应用程序时 仅在该模拟器上遇到了一些奇怪的问题 当我尝试一次又一次地运行该模拟器时 我的 MacBook 屏幕上出现了两个弹出警报窗口
  • 将 NSData 视频文件合并为一个视频文件

    我有一堆视频文件想要合并成一个视频文件 我正在使用 NSMutableData 来完成该任务 NSMutableData concatenatedData NSMutableData alloc init for int i 0 i lt
  • 使用 Xcode 6 和(可能)cocoapods 生成错误

    在构建使用 cocoapods 和最新 Xcode 6 GM 版本的 iOS 项目时 我收到以下静态分析器错误 error error reading pic error no analyzer checkers are associate
  • 在 iPhone 5 的横向模式下启动启动画面

    我们的通用应用程序仅适用于横向模式 我们需要为 iPhone 4 和 iPhone 5 添加启动画面 对于 iPhone 5 的肖像 我们使用 email protected cdn cgi l email protection 如何为 i
  • PreferredInterfaceOrientationForPresentation 必须返回受支持的界面方向 (iOS 6)

    我的应用程序窗口的根视图控制器是 UINavigationController 的子类 我已将此代码添加到类中 BOOL shouldAutorotate return self topViewController shouldAutoro
  • 如何安全地将 CGFloat 降低或提高到 int?

    我经常需要在地板或天花板上安装CGFloat to an int 用于计算数组索引 我永远看到的问题floorf theCGFloat or ceilf theCGFloat 是浮点不准确可能会带来麻烦 那如果我的CGFloat is 2
  • 如何在 Swift 编程中获得基于导航的模板功能

    我的项目需要一个导航控制器 并且我的应用程序最初有一个社交登录 一旦验证通过 用户将被推送到另一个视图 我在其中显示一个具有 2 个选项卡的选项卡控制器 我不知道如何在 Swift 编程中做到这一点 我已将视图控制器嵌入到导航控制器中 一旦
  • AVPlayerLooper 每次迭代后黑闪

    我正在使用 Apple 的示例代码在UICollectionViewCell背景 我在用着AVPlayerLooper 因为它是同一视频的迭代 我的问题是 当视频结束时 它会显示轻微的黑屏闪烁 也许它正在将视频搜索到0时间 我不确定 这是代
  • GIDSignIn 在提示前指定范围

    我在 iOS 上升级到 GoogleSignIn 6 0 但找不到在登录时指定登录范围的方法 我只能看到一个名为 addScopes 的 API 我可以在基本登录后指定范围 但这会导致两个单独的登录提示 这很奇怪 之前 我们可以简单地指定登
  • 我可以对图像进行动画处理吗 iOS8 LaunchScreen.xib

    问题 有没有方法可以为针对 iOS 8 1 部署的 Xcode 6 项目的 LaunchScreen xib 文件中的任何内容设置动画 Context 我正在寻找制作简单的动画来传达活动或在用户等待时分散他们的注意力 例子 加载栏 活动指示
  • 如何将图像放在 UIButton 中文本的右侧?

    如果可以避免的话 我不想使用子视图 我想要一个UIButton其中包含背景图像 文本和图像 现在 当我这样做时 图像位于文本的左侧 背景图像 文本和图像都有不同的高亮状态 最简单的解决方案 iOS 10 及更高版本 Swift button
  • 如何区分iphone4和iphone 3

    我正在尝试使用 cocos2d 引擎为 iphone 构建游戏 我想知道如何区分用户使用的是 iphone 4 还是 iphone 3 因为我想为 iphone4 加载高分辨率图形 为 iphone 3 加载低分辨率图形 我知道我是否在以下

随机推荐