码迷,mamicode.com
首页 > 其他好文 > 详细

自定义不规则的tabbarItem

时间:2015-07-07 10:46:22      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

第一步、找到TabBarController的viewWillAppear方法,注册TabBar即将出现、隐藏的通知

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotifications:) name:ZSYCurrentUserLoginStatusNotifyKey object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotifications:) name:ZSYTabbarWillHidenNotifyKey object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotifications:) name:ZSYTabbarWillShowNotifyKey object:nil];
        
}

 

第二步、接收通知回调

- (void)didReceiveNotifications:(NSNotification *)sender {
    
    if (!_middleImage) {
        _middleImage = [UIImage imageNamed:@"camera_button_take.png"];
    }
    
    if (!_middleButton) {
        [self addCenterButtonWithImage:[UIImage imageNamed:@"camera_button_take.png"] highlightImage:[UIImage imageNamed:@"tabBar_cameraButton_ready_matte.png"]];
    }
    
    if ([sender.name isEqualToString:ZSYTabbarWillHidenNotifyKey])
    {
        [self _hideTabBarMiddleButton];
    } else if ([sender.name isEqualToString:ZSYTabbarWillShowNotifyKey])
    {
        [self _showTabBarMiddleButton];
    }
}

 

第四步、TabbarController的viewWillDisAppear时,移除通知

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ZSYTabbarWillHidenNotifyKey object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ZSYTabbarWillShowNotifyKey object:nil];
}

 

第五步、显示TabBar中间的不规则按钮

- (void)_showTabBarMiddleButton {
    @weakify(self);
    [UIView animateWithDuration:0.2f animations:^{
        @strongify(self);
        CGRect frame = self.middleButton.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height - self.middleImage.size.height;
        self.middleButton.frame = frame;
    }];
}

 

第六步、隐藏TabBar中间的不规则按钮

- (void)_hideTabBarMiddleButton {
    
    @weakify(self);
    [UIView animateWithDuration:0.2f animations:^{
        @strongify(self);
        CGRect frame = self.middleButton.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height;
        self.middleButton.frame = frame;
    }];
}

 

第七步、在 [TabBarController.view addSubView: 自定义按钮作为tabbarItem ]

-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }
    
    [button addTarget:self action:@selector(middleButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    self.middleButton = button;
}

 

自定义不规则的tabbarItem

标签:

原文地址:http://www.cnblogs.com/xiongzenghui/p/4626184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!