iOS 开发自定义 UITabBarController 完整代码 (OC)
以下是一个简单的自定义 UITabBarController 的完整代码示例:
AppDelegate.h 文件:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m 文件:
#import "AppDelegate.h"
#import "CustomTabBarController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 创建自定义的 UITabBarController
CustomTabBarController *tabBarController = [[CustomTabBarController alloc] init];
// 设置根视图控制器为自定义的 UITabBarController
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
@end
CustomTabBarController.h 文件:
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController
@end
CustomTabBarController.m 文件:
#import "CustomTabBarController.h"
#import "CustomTabBar.h"
@interface CustomTabBarController () <CustomTabBarDelegate>
@end
@implementation CustomTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建自定义的标签栏
CustomTabBar *customTabBar = [[CustomTabBar alloc] init];
customTabBar.delegate = self;
// 替换系统的标签栏
[self setValue:customTabBar forKey:'tabBar'];
// 创建并添加子视图控制器
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
viewController1.tabBarItem.title = 'Tab 1';
viewController1.tabBarItem.image = [UIImage imageNamed:'tab1'];
viewController2.tabBarItem.title = 'Tab 2';
viewController2.tabBarItem.image = [UIImage imageNamed:'tab2'];
viewController3.tabBarItem.title = 'Tab 3';
viewController3.tabBarItem.image = [UIImage imageNamed:'tab3'];
self.viewControllers = @[viewController1, viewController2, viewController3];
}
#pragma mark - CustomTabBarDelegate
- (void)tabBar:(CustomTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index {
self.selectedIndex = index;
}
@end
CustomTabBar.h 文件:
#import <UIKit/UIKit.h>
@protocol CustomTabBarDelegate;
@interface CustomTabBar : UITabBar
@property (nonatomic, weak) id<CustomTabBarDelegate> delegate;
@end
@protocol CustomTabBarDelegate <NSObject>
- (void)tabBar:(CustomTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index;
@end
CustomTabBar.m 文件:
#import "CustomTabBar.h"
@implementation CustomTabBar
- (void)layoutSubviews {
[super layoutSubviews];
// 设置标签栏的 frame
CGFloat tabBarButtonWidth = self.bounds.size.width / self.items.count;
CGFloat tabBarButtonHeight = self.bounds.size.height;
CGFloat tabBarButtonY = 0;
for (int index = 0; index < self.items.count; index++) {
CGFloat tabBarButtonX = index * tabBarButtonWidth;
UIButton *tabBarButton = self.subviews[index];
tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonWidth, tabBarButtonHeight);
tabBarButton.tag = index;
[tabBarButton addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)tabBarButtonClicked:(UIButton *)sender {
if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemAtIndex:)]) {
[self.delegate tabBar:self didSelectItemAtIndex:sender.tag];
}
}
@end
此代码示例创建了一个自定义的 UITabBarController,并使用自定义的标签栏替换了系统的标签栏。自定义标签栏使用 UIButton 来表示每个标签按钮,并在点击按钮时触发代理方法通知 UITabBarController 切换选中的子视图控制器。
原文地址: http://www.cveoy.top/t/topic/o9jV 著作权归作者所有。请勿转载和采集!