zoukankan      html  css  js  c++  java
  • tabBar自定义

    有时系统的tabBar并不能满足我们的开发需求:

    这时,我们需要自定义一个tabBar。直接上代码:

     // 在tabBarController中用KVC更换掉系统tabBar
        [self setValue:[[TTTabBar alloc] init] forKeyPath:@"tabBar"];

    自定义tabBar:

    @interface TTTabBar()
    /** 相机按钮 */
    @property (nonatomic, weak) UIButton *cameraButton;
    @end
    
    @implementation XMGTabBar
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {// 添加相机按钮
            UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [cameraButton setBackgroundImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
            [cameraButton setBackgroundImage:[UIImage imageNamed:@"camera_click"] forState:UIControlStateHighlighted];
            cameraButton.size = publishButton.currentBackgroundImage.size;
            [self addSubview:cameraButton];
            self.cameraButton = cameraButton;
        }
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        self.cameraButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
        
        // 设置UITabBarButton的frame
        CGFloat buttonY = 0;
        CGFloat buttonW = self.width / 5;
        CGFloat buttonH = self.height;
        NSInteger index = 0;
        for (UIView *button in self.subviews) {
            if (![button isKindOfClass:[UIControl class]] || button == self.cameraButton) continue;
            
            // 计算按钮的x值
            CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
            button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
            
            // 增加索引
            index++;
        }
    }
  • 相关阅读:
    理解Cookie和Session的区别及使用
    数据库事务
    Mybatis和hibernate的优缺点比较
    MyBatis、JDBC相关知识
    JVM学习笔记(一,待整理)
    laravel运行url404错误
    电影TS、TC、SCR、R5、BD、HD等版本是什么意思
    mysql索引
    双系统更改启动顺序
    PHP Deprecated: Comments starting with '#' are deprecated in *.ini 警告解决办法
  • 原文地址:https://www.cnblogs.com/yintingting/p/4544421.html
Copyright © 2011-2022 走看看