2
A
回答
2
最近這讓我很生氣,所以我寫了自己的類來處理它,它適用於我嘗試過的每個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
希望有所幫助。
相關問題
- 1. UIColor的標籤欄項目未選中,並更改標籤欄顏色
- 2. 如何更改iOS中標籤欄中未選圖像的色調顏色?
- 3. 如何更改標籤欄項目的默認灰色顏色?
- 4. 更改未選定標籤欄圖標的顏色
- 5. 更改iOS 7中的標籤欄顏色?
- 6. 如何更改iOS 7中多個選項卡的標籤欄色調
- 7. 標籤欄項目色調顏色
- 8. 在iOS 7中,如何更改UIActionSheet中選項的顏色?
- 9. 更改未選中的UITabBar項目的顏色iOS 9
- 10. 更改標籤欄項目標題和顏色programmaticaly
- 11. Android:如何更改首選項中的選定項目顏色?
- 12. 如何更改特定文本的顏色選項標籤
- 13. 更改Android標籤欄的文本顏色未選擇
- 14. 自定義選項卡欄背景顏色。如何更改標籤欄背景的顏色?
- 15. 改變標籤欄未選中的圖標顏色迅速
- 16. 更改UICollectionView中標籤的顏色iOS
- 17. iOS的7/8如何設置標籤欄的文本顏色
- 18. 更改TintColor標籤欄在iOS 7.1中選擇的項目
- 19. 如何更改列表框中未選中的項目顏色
- 20. 如何在swift中更改標籤欄的色調顏色?
- 21. 如何更改iOS Swift中的默認標籤欄項目?
- 22. NavigationView(更改選定項目的顏色)
- 23. 更改標籤欄的背景顏色
- 24. 更改ios中狀態欄的標籤顏色
- 25. iOS 7上的標籤欄獨立的色調顏色?
- 26. 更改頂欄顏色和標籤欄文字顏色
- 27. 如何更改LongListMultiSelector中選定項目的顏色?
- 28. 如何更改LongListSelecter中選定項目的背景顏色?
- 29. 如何更改Windows Phone中選定項目的背景顏色?
- 30. 如何更改ListView中選定項目的背景顏色?