当前位置: 首页 > news >正文

ios 项目中设置左侧徽标

//

//  CategoryViewController.m

//  scxhgh

//

//  Created by xmkjsoft on 2024/7/16.

//

#import "CategoryViewController.h"

#import "SideMenuViewController.h" // 引入侧边栏控制器的头文件

#import "NavigationBarUtils.h"

@interface CategoryViewController ()

@property (nonatomic, strong) SideMenuViewController *sideMenuViewController;

@property (nonatomic, assign) BOOL isSideMenuVisible;

@end

@implementation CategoryViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    [self setupNavigationBarStyle];

    

    self.title=@"商品分类";

  

    [NavigationBarUtils setupNavigationBarStyleForViewController:self];

    // 设置导航栏带徽章的按钮

    [NavigationBarUtils setupNavigationBarWithBadgeForViewController:self withText:@"99+" selector:@selector(leftButtonTapped:)];

    // 初始化侧边菜单控制器

    self.sideMenuViewController = [[SideMenuViewController alloc] init];

    

}

- (void)leftButtonTapped:(UIButton *)sender {

    NSLog(@"Left Button Tapped!");

    // 这里可以添加逻辑,例如打开一个视图控制器或执行其他操作

    [self toggleSideMenu];

}

- (void)toggleSideMenu {

    if (self.isSideMenuVisible) {

        NSLog(@"关");

        [self hideSideMenu];

    } else {

        NSLog(@"开");

        [self showSideMenu];

    }

}

- (void)showSideMenu {

    CGFloat sideMenuWidth = 300;

    // 获取主窗口,兼容 iOS 12 及以下版本

    UIWindow *window;

    if (@available(iOS 13.0, *)) {

        window = UIApplication.sharedApplication.windows.firstObject;

    } else {

        window = UIApplication.sharedApplication.keyWindow;

    }

    CGRect sideMenuFrame = CGRectMake(0, 0, sideMenuWidth, CGRectGetHeight(window.bounds));

    self.sideMenuViewController.view.frame = sideMenuFrame;

    UIView *maskView = [[UIView alloc] initWithFrame:window.bounds];

    maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];

    [window addSubview:maskView];

    [window addSubview:self.sideMenuViewController.view];

    [UIView animateWithDuration:0.8 animations:^{

        CGRect newFrame = CGRectMake(0, 0, sideMenuWidth, CGRectGetHeight(window.bounds));

        self.sideMenuViewController.view.frame = newFrame;

    } completion:nil];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSideMenu)];

    [maskView addGestureRecognizer:tapGesture];

    self.isSideMenuVisible = YES;

}

- (void)hideSideMenu {

    // 获取主窗口,兼容 iOS 12 及以下版本

    UIWindow *window;

    if (@available(iOS 13.0, *)) {

        window = UIApplication.sharedApplication.windows.firstObject;

    } else {

        window = UIApplication.sharedApplication.keyWindow;

    }

    CGFloat sideMenuWidth = CGRectGetWidth(self.sideMenuViewController.view.frame);

    [UIView animateWithDuration:0.3 animations:^{

        CGRect newFrame = CGRectMake(-sideMenuWidth, 0, sideMenuWidth, CGRectGetHeight(window.bounds));

        self.sideMenuViewController.view.frame = newFrame;

    } completion:^(BOOL finished) {

        [self.sideMenuViewController.view removeFromSuperview];

        for (UIView *subview in window.subviews) {

            if ([subview isKindOfClass:[UIView class]] && subview.backgroundColor && CGColorEqualToColor(subview.backgroundColor.CGColor, [UIColor colorWithWhite:0 alpha:0.5].CGColor)) {

                [subview removeFromSuperview];

                break;

            }

        }

    }];

    self.isSideMenuVisible = NO;

}

- (void)setupNavigationBarStyle {

    // 设置导航栏背景色和文字颜色

    if (@available(iOS 15.0, *)) {

        // 创建导航栏外观对象

        UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc] init];

        

        // 设置导航栏背景色

        [navigationBarAppearance configureWithOpaqueBackground];

        navigationBarAppearance.backgroundColor = [UIColor colorWithRed:(255/255.0) green:(111/255.0) blue:(97/255.0) alpha:1.0];

        

        // 设置标题文字颜色

        navigationBarAppearance.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

        

        // 应用到导航栏

        self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;

        self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;

    } else {

        // iOS 14及以下版本的处理逻辑

        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:(255/255.0) green:(111/255.0) blue:(97/255.0) alpha:1.0];

        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

    }

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end


http://www.mrgr.cn/news/22578.html

相关文章:

  • linux运维常见命令行
  • 绿光扫码激光器定制多少钱?费用决定因素
  • 数据资产:新时代的财富密码
  • 长文本分块的新策略——后期分块(Late Chunking),让RAG应用“又准又高效“
  • 《数字图像处理(面向新工科的电工电子信息基础课程系列教材)》Chapter 1课件2024
  • Spring Boot 常用注解详解
  • LeetCode题练习与总结:最大正方形--221
  • 解决VSCode保存后未格式化文档的问题
  • Android Graphics 显示系统 - 图层的生命周期 Layer Lifecycle
  • 生成单据的流水号
  • AI一键生成 PPT
  • C#/.NET/.NET Core优质学习资料
  • FreeRTOS学习笔记(四)Freertos的中断管理及临界保护
  • C++ STL算法从入门及常见运用方法详解
  • 从C语言过渡到C++
  • 解锁高效驱动密码:SiLM8260A系列SiLM8260ABCS-DG 集成米勒钳位的双通道隔离驱动芯片
  • ISO 8601书写格式
  • 第四届长城杯部分wp
  • DeepSeek-V2.5:兼具通用能力和编码能力的新型开源模型
  • iotdbtool助力时序数据库IoTDB高效运维