自定义UITabBarController

作者: 39d4a1953758 | 来源:发表于2016-05-18 09:45 被阅读73次
@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");
}

相关文章

网友评论

    本文标题:自定义UITabBarController

    本文链接:https://www.haomeiwen.com/subject/qxqwrttx.html