ios 项目中设置左侧徽标

embedded/2024/10/18 21:25:30/

//

//  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.ppmy.cn/embedded/109538.html

相关文章

yum源配置与静态配置地址

网络yum源 备份配置文件 下载新的CentOS-Base.repo文件到/etc/yum.repos.d/目录下 执行yum clean all清除原有 yum 缓存 执行yum makecache(刷新缓存) 本地yum 将/etc/yum/repos.d/下的文件a都移走,此处移到了该目录下的bak中 找到光盘路…

Android 结合Opencv检测画面中的圆

记下来不用找 Opencv资源https://download.csdn.net/download/qq_37324563/89729678 版本可能比较老,凑合用吧 mBinding.cameraview.setCvCameraViewListener(object : CameraBridgeViewBase.CvCameraViewListener2 {/*** 当摄像机预览开始时,这个方法就会被调用…

网吧业务安全对抗(有源码)

网吧业务竞争激烈,网吧都会有以下系统软件。 无盘: 无盘是指没有硬盘。好处是统一维护管理和节约成本。本人研究无盘好几年,后面会专门发帖介绍。 计费: 是指收费系统。 营销软件: 包括销售饮品、‌零食和向客户发送电子邮件营销和短信营销等。产品如…

深入理解FastAPI的response_model:自动化数据验证与文档生成

使用 FastAPI 的 response_model 参数 在构建 RESTful API 时,确保数据的一致性和正确性是非常重要的。FastAPI 提供了强大的工具来帮助开发者实现这一目标。其中一个关键特性是 response_model 参数,它允许开发者定义期望的响应格式,并自动…

【自考zt】【软件工程】【21.04】(部分)

一、单选 二、填空 三、简答 四、应用 小结:

socket网络编程——原理(详细阐述)

目录 ☀️0.前言 🌤️1.文件描述符(fd) 🌤️2.socket()函数详解 🌤️3.bind()函数详解 🌤️4.listen()和accept()函数详解 🌤️5.recv()和send()函数详解 🌤️6.close()函数详…

【H2O2|全栈】关于HTML(2)HTML基础(一)

HTML相关知识 目录 前言 准备工作 标签的具体分类(一) 本文中的标签在什么位置使用? 属性 标题标签 段落标签 文本格式化标签 分类汇总 计算机输出标签 ​编辑分类汇总 引文,引用标签 分类汇总 预告和回顾 UI设计…

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree …