iOS(OC)菜单头部封装

  1. iOS(OC)菜单头部封装
    .h代码如下;
@interface TitleScrollView : UIView<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *titleScroller;
@property (nonatomic,strong)UIButton *selectBtn;
@property (nonatomic,strong)NSArray *titleArr;
@property(nonatomic,copy)void(^clickTitleScrollView)(NSInteger);//tag
@end

.m代码如下;

#import "TitleScrollView.h"

@implementation TitleScrollView
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self creatScrollerView];
    }
    return self;
}
-(void)setTitleArr:(NSArray *)titleArr{
    _titleArr = titleArr;
    for (UIView *view in self.titleScroller.subviews) {
           [view removeFromSuperview];
       }
     CGFloat totalWidth = 20;
     for (int i = 0 ; i < self.titleArr.count; i++){
      CGRect rect = [self.titleArr[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 44) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
      UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      titleBtn.frame = CGRectMake(totalWidth, 0, rect.size.width, 44);
      totalWidth = totalWidth + rect.size.width + 20;
      titleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
      [titleBtn setTitle:self.titleArr[i] forState:UIControlStateNormal];
      [titleBtn setTitleColor:[UIColor colorWithRed:166/255.0 green:167/255.0 blue:168/255.0 alpha:1.0] forState:UIControlStateNormal];
      [titleBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
      titleBtn.tag = 400 + i;
      [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
      [self.titleScroller addSubview:titleBtn];
      if (i == 0){
          self.selectBtn = titleBtn;
          titleBtn.selected = YES;
          UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(titleBtn.center.x-20, 41,40 , 3)];
          lineView.backgroundColor = [UIColor blueColor];
          lineView.tag = 222;
          [self.titleScroller addSubview:lineView];
      }
    }
      if (totalWidth > kScreenWidths){
          self.titleScroller.contentSize = CGSizeMake(totalWidth,44);
      }
          self.titleScroller.showsHorizontalScrollIndicator = NO;
}
-(void)creatScrollerView{
      self.titleScroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,kScreenWidths ,44)];
      self.titleScroller.backgroundColor = [UIColor whiteColor];
      self.titleScroller.layer.shadowColor = [UIColor darkGrayColor].CGColor;
      self.titleScroller.layer.shadowOffset = CGSizeMake(0, 5);
      self.titleScroller.layer.shadowOpacity = 0.3;
      self.titleScroller.layer.shadowRadius = 3;
      self.titleScroller.layer.masksToBounds = NO;
      [self addSubview:self.titleScroller];
}
-(void)titleBtnClick:(UIButton *)sender{
    if (sender != self.selectBtn){
        self.selectBtn.selected = NO;
        sender.selected = YES;
        self.selectBtn = sender;
        UIView *lineView = [self viewWithTag:222];
        lineView.frame = CGRectMake(sender.center.x-20, 41, 40, 3);
        if (CGRectGetMaxX(sender.frame)>kScreenWidths){
            self.titleScroller.contentOffset = CGPointMake(CGRectGetMaxX(sender.frame)-kScreenWidths+20, 0);
        }else {
            self.titleScroller.contentOffset = CGPointMake(0, 0);
        }
       if(self.clickTitleScrollView!= nil) {
           self.clickTitleScrollView(sender.tag);
       }
    }
}

2:控制器使用方法:

#import "QTItemViewController.h"
#import "TitleScrollView.h"
#import "view1.h"
#import "view2.h"
#import "view3.h"
#import "view4.h"
#import "view5.h"
#import "view6.h"
@interface QTItemViewController ()
@property (nonatomic,strong)NSArray *titleArr;
@property (nonatomic,strong)TitleScrollView *titleScroller;
@property (nonatomic,strong)UIButton *selectBtn;
@property (nonatomic,strong)view1 *view1;
@property (nonatomic,strong)view2 *view2;
@property (nonatomic,strong)view3 *view3;
@property (nonatomic,strong)view4 *view4;
@property (nonatomic,strong)view5 *view5;
@property (nonatomic,strong)view6 *view6;
@end

@implementation QTItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"演示demo";
    self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:246/255.0 blue:252/255.0 alpha:1.0];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.titleScroller = [[TitleScrollView alloc]initWithFrame:CGRectMake(0,kNavigationBarHeight,kScreenWidths,44)];
    self.titleScroller.backgroundColor = [UIColor redColor];
    __weak typeof(self) weakSelf = self;
    self.titleScroller.clickTitleScrollView = ^(NSInteger buttonIndex) {
        NSLog(@"%ld",(long)buttonIndex);
        [weakSelf clickButton:buttonIndex];
    };
    [self.view addSubview:self.titleScroller];
    [self getData];
    [self clickButton:400];
}
-(void)getData{
   self.titleArr = [NSArray arrayWithObjects:@"活动1",@"活动2",@"活动3",@"活动4",@"活动5", @"活动6",nil];
   self.titleScroller.titleArr = self.titleArr;
}
-(void)clickButton:(NSInteger)senderTag{
     if ([self.view viewWithTag:100] != nil) {
            [[self.view viewWithTag:100] removeFromSuperview];
        }
     if ([self.view viewWithTag:101] != nil) {
        [[self.view viewWithTag:101] removeFromSuperview];
     }
    if ([self.view viewWithTag:102] != nil) {
               [[self.view viewWithTag:102] removeFromSuperview];
           }
    if ([self.view viewWithTag:103] != nil) {
       [[self.view viewWithTag:103] removeFromSuperview];
    }
    if ([self.view viewWithTag:104] != nil) {
               [[self.view viewWithTag:104] removeFromSuperview];
           }
    if ([self.view viewWithTag:105] != nil) {
       [[self.view viewWithTag:105] removeFromSuperview];
    }
    switch (senderTag) {
        case 400:
            if (!_view1) {
                _view1 = [[view1 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
                _view1.tag = 100;
                }
            [self.view addSubview:self.view1];
            break;
        case 401:
           if (!_view2) {
              _view2 = [[view2 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
              _view2.tag = 101;
              }
          [self.view addSubview:self.view2];
            break;
        case 402:
           if (!_view3) {
               _view3 = [[view3 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
               _view3.tag = 102;
               }
           [self.view addSubview:self.view3];
           break;
       case 403:
          if (!_view4) {
             _view4 = [[view4 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
             _view4.tag = 103;
             }
         [self.view addSubview:self.view4];
           break;
       case 404:
          if (!_view5) {
              _view5 = [[view5 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
              _view5.tag = 104;
              }
          [self.view addSubview:self.view5];
          break;
        case 405:
         if (!_view6) {
            _view6= [[view6 alloc]initWithFrame:CGRectMake(0,44 + kNavigationBarHeight, kScreenWidth,kScreenHeight - kNavigationBarHeight)];
            _view6.tag = 105;
            }
        [self.view addSubview:self.view6];
          break;
        default:
            break;
    }
}

3.效果展示:


Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-30 at 15.49.37.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,248评论 8 265
  • 视频处理 BeautifyFaceDemo[https://github.com/Guikunzhi/Beauti...
    香橙柚子阅读 2,498评论 0 12
  • OC语言基础 1.类与对象 类方法 OC的类方法只有2种:静态方法和实例方法两种 在OC中,只要方法声明在@int...
    奇异果好补阅读 4,230评论 0 11
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 123,175评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,018评论 0 4