IOS学习(十六)自定义tabBar
来源:互联网 发布:联想x270指纹识别软件 编辑:程序博客网 时间:2024/06/03 17:38
#import "CustomTabBarItem.h"@implementation CustomTabBarItem+ (instancetype) itemWithFrame:(CGRect)frame title:(NSString *)title{ CustomTabBarItem *item = [CustomTabBarItem buttonWithType:UIButtonTypeCustom]; [item setFrame:frame]; [item setTitle:title forState:UIControlStateNormal]; [item.titleLabel setFont:[UIFont systemFontOfSize:17]]; [item setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [item setBackgroundImage:[UIImage imageNamed:@"n"] forState:UIControlStateNormal]; [item setBackgroundImage:[UIImage imageNamed:@"s"] forState:UIControlStateSelected]; return item;}@end
#import <UIKit/UIKit.h>@class CustomTabBar;//自定义协议,实现与其他类通信@protocol CustomTabBarDelegate <NSObject>- (void) tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index;@end@interface CustomTabBar : UIView@property(nonatomic, weak) id<CustomTabBarDelegate> delegate;+ (instancetype) tabBarWithTites:(NSArray *)titles;@end
#import "CustomTabBar.h"#import "CustomTabBarItem.h"static const CGFloat TabBarHeight = 44;@interface CustomTabBar()@property(nonatomic, strong) NSArray *titles;@property(nonatomic, weak) CustomTabBarItem *preSelectedItem;@end@implementation CustomTabBar+ (instancetype) tabBarWithTites:(NSArray *)titles{ CustomTabBar *tabBar = [[CustomTabBar alloc] init]; [tabBar setTitles:titles]; [tabBar setBackgroundColor:[UIColor grayColor]]; return tabBar;}- (void)willMoveToSuperview:(UIView *)newSuperview{ self.frame = CGRectMake(0, CGRectGetMaxY(newSuperview.frame) - TabBarHeight, CGRectGetWidth(newSuperview.frame), TabBarHeight); //必须是在这里创建items,不然无法获取其frame信息 [self addItems];}- (void) addItems{ CGFloat count = self.titles.count; CGFloat height = CGRectGetHeight(self.bounds); CGFloat width = CGRectGetWidth(self.bounds) / count; for (NSInteger i = 0 ; i < count ; i++){ CustomTabBarItem *item = [CustomTabBarItem itemWithFrame:CGRectMake(i * width, 0, width, height) title:self.titles[i]]; [item setTag:i]; [self addSubview:item]; //设置监听,点击按钮时 [item addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside]; }}- (void)itemSelected:(CustomTabBarItem *)send{ [self.preSelectedItem setSelected:NO]; [send setSelected:YES]; [self setPreSelectedItem:send]; [self.delegate tabBar:self itemSelectedAtIndex:send.tag];}@end
#import "MainViewController.h"#import "CustomTabBar.h"@interface MainViewController ()<CustomTabBarDelegate>@property(nonatomic, strong) UILabel *label;@property(nonatomic, strong) NSArray *titles;@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.titles = @[@"OC", @"JAVA", @"PHP"]; CustomTabBar *tabBar = [CustomTabBar tabBarWithTites:self.titles]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 200)]; [self.label setBackgroundColor:[UIColor grayColor]]; [self.label setFont:[UIFont systemFontOfSize:60]]; [self.label setTextColor:[UIColor yellowColor]]; [self.label setTextAlignment:NSTextAlignmentCenter]; [self.label setText:@"OC"]; [self.view addSubview:tabBar]; [self.view addSubview:self.label]; [tabBar setDelegate:self]; }- (void)tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index{ [self.label setText:self.titles[index]];}@end
0 0
- IOS学习(十六)自定义tabBar
- iOS自定义tabbar(没有tabbar上的黑线)
- iOS 根据后台设置tabbar (自定义tabbar)
- IOS之自定义tabbar
- ios 自定义TabBar
- iOS 自定义tabbar
- iOS Swift 自定义tabbar
- IOS 自定义tabbar
- iOS自定义tabbar
- iOS 自定义tabBar
- iOS -- 自定义TabBar
- iOS 开发 自定义Tabbar
- iOS自定义TabBar
- ios中自定义tabBar
- ios中自定义tabBar
- IOS 自定义Tabbar
- iOS自定义tabBar
- iOS自定义tabbar
- web页面
- 微信小程序学习(八)之checkbox组件
- STM32之SPI的使用
- 二分图基础知识+匈牙利算法模版
- Java程序员必会的13种热门技能
- IOS学习(十六)自定义tabBar
- Class.forName加载JDK类(Srtring)
- typedef 函数指针的用法
- jenkins(1)------linux安装jenkins
- 单链表及基本运算 && 双向链表及基本运算 && 静态链表
- Java程序员须知的Java在各大领域的应用及前景
- IOS消息推送
- python基础
- 一台电脑上datagurad搭建过程