zoukankan      html  css  js  c++  java
  • 自定义导航栏(navigationBar)

    为了美观性,一般设置通用的navigationBar

    创建基础控件

        UIView *naviView;
        UIImageView *naviImageView;
        UILabel *naviLabel;
        UIButton *naviLeftBtn;
    

    设置控件位置(Frame)

        CGRect naviFrame = CGRectMake(0, 0, 320, 64);
        CGRect backBtnFrame = CGRectMake(10, 31, 30, 21);
        CGRect labelFrame = CGRectMake(0, 31, 320, 21);
    

     将Frame应用到控件上

        naviView = [[UIView alloc] initWithFrame:naviFrame];
        naviImageView = [[UIImageView alloc] initWithFrame:naviFrame];
        naviLabel = [[UILabel alloc] initWithFrame:labelFrame];
    

     设置标题样式

        naviLabel.backgroundColor = [UIColor clearColor];
        naviLabel.text = title;
        //    naviLabel.textColor = color;
        [naviLabel setTintColor:color];
        naviLabel.textAlignment = NSTextAlignmentCenter;
    

     将imageView和label添加到主View上

        [naviView addSubview:naviImageView];
        [naviView addSubview:naviLabel];
    

     自定义左侧返回按钮

      naviLeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      [naviLeftBtn setTintColor:color];
      [naviLeftBtn setFrame:backBtnFrame];
      [naviLeftBtn setImage:[UIImage imageNamed:@"navi_backBtn_normal.png"] forState:UIControlStateNormal];
      [naviLeftBtn setImage:[UIImage imageNamed:@"navi_backBtn_selected.png"] forState:UIControlStateHighlighted];
      [naviLeftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
      [naviView addSubview:naviLeftBtn];

    总代码

    #pragma mark 设置导航条及左右按钮
    -(UIView *)navigationBarWithTitle:(NSString *)title titleColor:(UIColor *)color isShowBackBtn:(BOOL)isShow rightBtn:(UIButton *)right {
        //x,y,w,h
        CGRect naviFrame = CGRectMake(0, 0, 320, 64);
        CGRect backBtnFrame = CGRectMake(10, 31, 30, 21);
        CGRect rightBtnFrame = CGRectMake(right.frame.origin.x, 27, right.frame.size.width, right.frame.size.height);
        CGRect labelFrame = CGRectMake(0, 31, 320, 21);
        
    //    if(!iOS7)
    //    {
    //        naviFrame = CGRectMake(0, 0, 320, 44);
    //        labelFrame = CGRectMake(0, 11, 320, 21);
    //        backBtnFrame = CGRectMake(10, 11, 21, 21);
    //        rightBtnFrame = CGRectMake(right.frame.origin.x, 11, right.frame.size.width, right.frame.size.height);
    //    }
        //    naviImageView.image = [UIImage imageNamed:naviImage_6];
        naviView = [[UIView alloc] initWithFrame:naviFrame];
        
        naviImageView = [[UIImageView alloc] initWithFrame:naviFrame];
        naviImageView.backgroundColor = [UIColor blueColor];
        naviLabel = [[UILabel alloc] initWithFrame:labelFrame];
        naviLabel.backgroundColor = [UIColor clearColor];
        naviLabel.text = title;
        //    naviLabel.textColor = color;
        [naviLabel setTintColor:color];
        naviLabel.textAlignment = NSTextAlignmentCenter;
        
        [naviView addSubview:naviImageView];
        [naviView addSubview:naviLabel];
        
        if(isShow)
        {
            naviLeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [naviLeftBtn setTintColor:color];
            [naviLeftBtn setFrame:backBtnFrame];
            [naviLeftBtn setImage:[UIImage imageNamed:@"navi_backBtn_normal.png"] forState:UIControlStateNormal];
            [naviLeftBtn setImage:[UIImage imageNamed:@"navi_backBtn_selected.png"] forState:UIControlStateHighlighted];
            [naviLeftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            [naviView addSubview:naviLeftBtn];
        }
        if(right)
        {
            [right setFrame:rightBtnFrame];
            [naviView addSubview:right];
        }
        [self.view addSubview:naviView];
        return naviView;
    }
    
  • 相关阅读:
    Python机器学习(五十三)SciPy 特殊函数
    Python机器学习(五十二)SciPy 基础功能
    Ofbiz项目学习——阶段性小结——服务返回结果
    Ofbiz项目学习——阶段性小结——视图
    Java-Eclipse-findbugs-sonar学习
    高性能MySQL
    Excel——读取文件后——组装成待插入数据库数据——实体映射模式
    Excel——读取——导出目录
    导入Excel——解析Excel——优化
    导入Excel——解析Excel
  • 原文地址:https://www.cnblogs.com/saurik/p/4979388.html
Copyright © 2011-2022 走看看