【新浪微博項目】05--自訂TabBarButton,05--tabbarbutton
1.TabBar的階層
2.自訂TabBar的背景可以將一個很短的圖片拉長作為背景圖片,方法:
self.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageWithName:@"tabbar_background"]];
- (id)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { if (!iOS7) { // 非iOS7下,設定tabbar的背景 self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"tabbar_background"]]; } } return self;}設定UIImage中imageWithName中的自動提示:
可以通過拷貝Item,然後修改Value中的值為imageWithName:
3.設定點擊選中的按鈕
@property (nonatomic,weak) IWTabBarButton *selectedButton;
- (void)addTabBarButtonWithItem:(UITabBarItem *)item{ // 1.建立按鈕 IWTabBarButton *button = [[IWTabBarButton alloc] init]; [self addSubview:button]; // 2.設定資料 button.item = item; // 3.監聽按鈕點擊 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown]; // 4.預設選中第0個按鈕 if (self.subviews.count == 1) { [self buttonClick:button]; }}
- (void)buttonClick:(IWTabBarButton *)button{ //1.當年被選中的按鈕取消選中 self.selectedButton.selected = NO; //2.被點擊的按鈕設定選中 button.selected = YES; //3.被點擊的按鈕變為選中按鈕 self.selectedButton = button;}
4.自訂按鈕①設定按鈕內部圖片在上面,文字描述在下面
// 內部圖片的frame- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageW = contentRect.size.width; CGFloat imageH = contentRect.size.height * IWTabBarButtonImageRatio; return CGRectMake(0, 0, imageW, imageH);}// 內部文字的frame- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat titleY = contentRect.size.height * IWTabBarButtonImageRatio; CGFloat titleW = contentRect.size.width; CGFloat titleH = contentRect.size.height - titleY; return CGRectMake(0, titleY, titleW, titleH);}②去掉高亮
// 重寫去掉高亮狀態- (void)setHighlighted:(BOOL)highlighted {}
③設定圖片和文字狀態
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 表徵圖置中 self.imageView.contentMode = UIViewContentModeCenter; // 文字置中 self.titleLabel.textAlignment = NSTextAlignmentCenter; // 字型大小 self.titleLabel.font = [UIFont systemFontOfSize:11]; // 文字顏色 [self setTitleColor:IWTabBarButtonTitleColor forState:UIControlStateNormal]; [self setTitleColor:IWTabBarButtonTitleSelectedColor forState:UIControlStateSelected]; if (!iOS7) { // 非iOS7下,設定按鈕選中時的背景 [self setBackgroundImage:[UIImage imageWithName:@"tabbar_slider"] forState:UIControlStateSelected]; } } return self;}
④對按鈕的屬性進行封裝
@property (nonatomic,strong) UITabBarItem *item;
// 設定item- (void)setItem:(UITabBarItem *)item{ _item = item; [self setTitle:item.title forState:UIControlStateNormal]; [self setImage:item.image forState:UIControlStateNormal]; [self setImage:item.selectedImage forState:UIControlStateSelected];}
5.TabBar內部的點擊通過代理或者通知告訴控制器在TarBar的標頭檔中添加代理對象:
#import <UIKit/UIKit.h>@class IWTabBar;@protocol IWTabBarDelegate <NSObject>@optional- (void)tabBar:(IWTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to;@end@interface IWTabBar : UIView- (void)addTabBarButtonWithItem:(UITabBarItem *)item;@property (nonatomic, weak) id<IWTabBarDelegate> delegate;@end
在點擊按鈕的時候,通知代理,這裡給按鈕綁定tag
/** * 監聽按鈕點擊 */- (void)buttonClick:(IWTabBarButton *)button{ // 1.通知代理 if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) { [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag]; } // 2.設定按鈕的狀態 //1.當年被選中的按鈕取消選中 self.selectedButton.selected = NO; //2.被點擊的按鈕設定選中 button.selected = YES; //3.被點擊的按鈕變為選中按鈕 self.selectedButton = button;}在MRRootTarBarController中獲得代理
@interface MRRootTabBarController ()<IWTabBarDelegate>
獲得代理:
customTabBar.delegate =self;
/** * 監聽tabbar按鈕的改變 * @param from 原來選中的位置 * @param to 最新選中的位置 */- (void)tabBar:(IWTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to{ self.selectedIndex = to;}