常见新闻App顶部菜单标签栏

这里写图片描述
这是我们常见的新闻类App,点击顶部菜单切换内容。

1.构建顶部菜单栏

/**
 *  设置顶部的标签栏
 */
- (void)setupTitlesView
{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
    titlesView.width = self.view.width;
    titlesView.height = 35;
    titlesView.y = 64;
    [self.view addSubview:titlesView];

    // 底部红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.y = titlesView.height - indicatorView.height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;

    // 内部子标签
    NSArray *titles = @[@"全部 ",@"视频",@"声音",@"图片",@"段子"];
    CGFloat height = titlesView.height;
    CGFloat width = titlesView.width / titles.count;

    for (NSInteger i=0; i<titles.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.height = height;
        button.width = width;
        button.x = i * button.width;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];

        // 默认点击了第一个按钮
        if (i == 0) {
            [self titleClick:button];
        }
    }
}

分析:大的UIView内部,包含了若干菜单(按钮UIButton),另外还有一个View来做 指示器。

/**
 *  标签底部红色指示器
 */
@property(nonatomic,weak)UIView *indicatorView;

2.点击了菜单要做的事情
点击按钮都会调用titleClick: 这个方法

/**
 *  点击了标签栏里的按钮
 */
- (void)titleClick:(UIButton *)button
{
    // 修改按钮的状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;

    // 动画
    [UIView animateWithDuration:.025 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
}

最后效果:
这里写图片描述

在uni-app中,你可以使用uni-ui组件库中的mescroll-uni来实现顶部分类菜单的效果。具体步骤如下: 1. 安装uni-ui组件库 在HBuilderX中,点击工具上的“插件市场”,搜索并安装uni-ui组件库。 2. 引入mescroll-uni组件 在需要使用顶部分类菜单的页面中,通过以下代码引入mescroll-uni组件: ```html <template> <view> <!-- mescroll-uni组件 --> <mescroll-uni :up="upOption" :down="downOption"> <!-- 内容区域 --> </mescroll-uni> </view> </template> <script> import MescrollUni from '@/uni_modules/mescroll-uni/components/mescroll-uni.vue' export default { components: { MescrollUni } } </script> ``` 3. 配置mescroll-uni组件 在页面的script标签中,配置mescroll-uni组件的选项,其中包括顶部分类菜单的数据和事件处理函数。示例代码如下: ```javascript <script> export default { data() { return { // 顶部分类菜单数据 tabs: [{ id: 1, name: '分类1' }, { id: 2, name: '分类2' }, { id: 3, name: '分类3' }], // 当前选中的分类菜单索引 activeIndex: 0 } }, methods: { // 顶部分类菜单切换事件处理函数 onTabClick(index) { this.activeIndex = index // 刷新数据 this.$refs.mescrollUni.refresh() }, // 上拉加载数据 loadMore() { // 加载数据的逻辑 }, // 下拉刷新数据 refresh() { // 刷新数据的逻辑 } }, computed: { // mescroll-uni组件的up选项 upOption() { return { callback: this.loadMore } }, // mescroll-uni组件的down选项 downOption() { return { isBoth: true, auto: true, callback: this.refresh } } } } </script> ``` 4. 渲染顶部分类菜单 在mescroll-uni组件中添加顶部分类菜单的HTML结构和事件绑定,示例代码如下: ```html <template> <view> <!-- 顶部分类菜单 --> <view class="tab-bar"> <view class="tab-item" :class="{active: activeIndex === index}" v-for="(tab, index) in tabs" :key="tab.id" @click="onTabClick(index)"> {{tab.name}} </view> </view> <!-- mescroll-uni组件 --> <mescroll-uni :up="upOption" :down="downOption"> <!-- 内容区域 --> </mescroll-uni> </view> </template> <style> .tab-bar { display: flex; justify-content: space-between; background-color: #fff; padding: 10rpx; border-bottom: 1rpx solid #e5e5e5; } .tab-item { font-size: 28rpx; color: #333; padding: 10rpx; border-radius: 10rpx; transition: all .3s ease; } .tab-item.active { background-color: #f5f5f5; color: #ff7f50; } </style> ``` 通过以上步骤,你就可以在uni-app中实现一个顶部分类菜单的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值