自定義UITabBarController

@interface AppDelegate ()

@property (nonatomic, retain)UITabBarController *tabBarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    UINavigationController *firstNaVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    firstNaVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1000];
    
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    UINavigationController *secondNaVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
    secondNaVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1001];
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[firstNaVC, secondNaVC];
    
    [self.window addSubview:self.tabBarController.view];
    
    self.window.rootViewController = self.tabBarController;
    
    [self addCenterTabbarButton];
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)addCenterTabbarButton {
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [centerButton setImage:[UIImage imageNamed:@"camera.png"] forState:0];
    UIImage *centerImage = [UIImage imageNamed:@"camera.png"];
    centerButton.frame = CGRectMake(0, 0, centerImage.size.width, centerImage.size.height);
    [centerButton setImage:centerImage forState:0];
    [centerButton addTarget:self action:@selector(centerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    CGFloat imageHeight = centerImage.size.height;
    CGFloat barHeight = self.tabBarController.tabBar.frame.size.height;
    CGFloat delta = imageHeight - barHeight;
    
    if (delta <= 0) {
        centerButton.center = self.tabBarController.tabBar.center;
    } else {
        CGPoint center = self.tabBarController.tabBar.center;
        center.y = center.y - delta / 2.0;
        centerButton.center = center;
    }
    [self.tabBarController.view addSubview:centerButton];
}

- (void)centerButtonClick:(UIButton *)button {
    NSLog(@"centerButton did clicked");
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容