Files
xj-ios/XinjiangProject/XinjiangProject/Project/Base/HQBaseViewController.m
T
2025-06-30 09:33:34 +08:00

256 lines
7.6 KiB
Objective-C

//
// HQBaseViewController.m
// healthQuestion
//
// Created by xulei on 2022/8/11.
//
#import "HQBaseViewController.h"
#import "HQLoginViewController.h"
@interface HQBaseViewController ()<MBProgressHUDDelegate>
@end
@implementation HQBaseViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
_StatusBarStyle=StatusBarStyle;
[self setNeedsStatusBarAppearanceUpdate];
}
//
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//是否显示返回按钮
self.isShowLiftBack = YES;
//是否需要登录/默认no
//默认导航栏样式:黑字
self.StatusBarStyle = UIStatusBarStyleLightContent;
}
/**
* 是否显示返回按钮
*/
- (void) setIsShowLiftBack:(BOOL)isShowLiftBack
{
_isShowLiftBack = isShowLiftBack;
NSInteger VCCount = self.navigationController.viewControllers.count;
//下面判断的意义是 当VC所在的导航控制器中的VC个数大于1 或者 是present出来的VC时,才展示返回按钮,其他情况不展示
if (isShowLiftBack && ( VCCount > 1 || self.navigationController.presentingViewController != nil)) {
[self addNavigationItemWithImageNames:@[@"whiteBack_icon"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
} else {
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem * NULLBar=[[UIBarButtonItem alloc]initWithCustomView:[UIView new]];
self.navigationItem.leftBarButtonItem = NULLBar;
}
}
- (void)backBtnClicked
{
if (self.presentingViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark ————— 导航栏 添加图片按钮 —————
/**
导航栏添加图标按钮
@param imageNames 图标数组
@param isLeft 是否是左边 非左即右
@param target 目标
@param action 点击方法
@param tags tags数组 回调区分用
*/
- (void)addNavigationItemWithImageNames:(NSArray *)imageNames isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
NSMutableArray * items = [[NSMutableArray alloc] init];
//调整按钮位置
// UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// //将宽度设为负值
// spaceItem.width= -5;
// [items addObject:spaceItem];
NSInteger i = 0;
for (NSString * imageName in imageNames) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
btn.tag = [tags[i++] integerValue];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
[items addObject:item];
}
if (isLeft) {
self.navigationItem.leftBarButtonItems = items;
} else {
self.navigationItem.rightBarButtonItems = items;
}
}
#pragma mark ————— 导航栏 添加文字按钮 —————
- (NSMutableArray<UIButton *> *)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
NSMutableArray * items = [[NSMutableArray alloc] init];
NSMutableArray * buttonArray = [NSMutableArray array];
NSInteger i = 0;
for (NSString * title in titles) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = SYSTEMFONT(15);
btn.tag = [tags[i++] integerValue];
[btn sizeToFit];
//设置偏移
if (isLeft) {
[btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
}else{
[btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
}
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
[items addObject:item];
[buttonArray addObject:btn];
}
if (isLeft) {
self.navigationItem.leftBarButtonItems = items;
} else {
self.navigationItem.rightBarButtonItems = items;
}
return buttonArray;
}
#pragma mark - 屏幕旋转
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//当前支持的旋转类型
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
// 是否支持旋转
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// 默认进去类型
return UIInterfaceOrientationPortrait;
}
#pragma mark - ProgressHUD
/**
* 展示轮型加载器
*
* @param string 展示的内容
* @param hidden yes自动隐藏 no不隐藏
*/
- (void)showMBProgressHUDWithString:(NSString *)string autoHidden:(BOOL)hidden
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:kApplication.keyWindow animated:YES];
HUD.backgroundColor = RGBA(0, 0, 0, 0.5);
if (string != nil && ![string isEqualToString:@""])
{
HUD.label.text = string;
}
if (hidden)
{
[HUD hideAnimated:YES afterDelay:2];
}
}
/**
* 展示提醒文字后1.5秒后消失
*
* @param string 展示信息
*/
- (void)showMBProgressHUDOnlyWithString:(NSString *)string
{
[self showMBProgressHUDOnlyWithString:string haveDelegate:NO];
}
- (void)showMBProgressHUDOnlyWithString:(NSString *)string haveDelegate:(BOOL)haveDelegate
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.mode = MBProgressHUDModeText;//only show label
if (!ValidStr(string)) {
HUD.label.text = string;
}
HUD.label.textColor = KWhiteColor;
HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
HUD.bezelView.color = UIColor.blackColor;
HUD.userInteractionEnabled = NO;
if (haveDelegate)
{
HUD.delegate = self;
}
[HUD hideAnimated:YES afterDelay:3];
}
- (void)showMBProgressHUDOnlyWithString:(NSString *)string autoHidden:(BOOL)hidden
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:kApplication.keyWindow animated:YES];
HUD.mode = MBProgressHUDModeText;//only show label
HUD.label.text = string;
if (hidden)
{
[HUD hideAnimated:YES afterDelay:1.5];
}
}
- (void)showMBProgressHUDWithString:(NSString *)string detailString:(NSString *)detailString
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.mode = MBProgressHUDModeText;
HUD.label.text = string;
HUD.detailsLabel.text = detailString;
[HUD hideAnimated:YES afterDelay:1.5];
}
- (void)hiddenAllMBProgressHUD
{
[MBProgressHUD hideHUDForView:kApplication.keyWindow animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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