You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

258 lines
11 KiB

//
// QuotationViewController.m
// HC
//
// Created by huilinLi on 2025/11/26.
//
#import "QuotationViewController.h"
#import "CountryModel.h"
#import "MLXYViewController.h" // 马来西亚
#import "CNViewController.h" // 中国
#import "CommonTabBar.h"
#import "HomeViewController.h"
#import "MyViewController.h"
@interface QuotationViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) CommonTabBar *commonTabBar;
@property (nonatomic, strong) NSArray<CountryModel *> *countries; // 国家列表
@property (nonatomic, strong) UIViewController *currentChildVC; // 当前显示的子控制器
@property (nonatomic, strong) UICollectionView *countryTabBar; // 国家选择标签栏
@property (nonatomic, strong) UIView *contentContainer; // 子控制器容器
@property (nonatomic, assign) NSInteger selectedIndex; // 当前选中的国家索引
// 顶部
@property (nonatomic, strong) UIImageView *logoImage; // logo
@property (nonatomic, strong) UITextField *searchField; // 搜索框
@property (nonatomic, strong) UIImageView *flagIcon; // 国旗
@end
@implementation QuotationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self initCountries];
[self setupSubviews];
[self setupConstraints];
// 默认选中第一个
self.selectedIndex = 0;
[self switchToCountryAtIndex:self.selectedIndex];
}
#pragma mark - 初始化国家数据
- (void)initCountries {
self.countries = @[
[CountryModel modelWithCode:@"MY"
name:@"马来西亚"
vcClassName:NSStringFromClass([MLXYViewController class])],
[CountryModel modelWithCode:@"CN"
name:@"中国"
vcClassName:NSStringFromClass([CNViewController class])]
];
}
#pragma mark - 创建子视图
- (void)setupSubviews {
self.logoImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
self.logoImage.contentMode = UIViewContentModeScaleAspectFit;
self.logoImage.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.logoImage];
self.searchField = [[UITextField alloc] init];
self.searchField.placeholder = @"股票名称/代码";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"股票名称/代码" attributes:@{
NSForegroundColorAttributeName: [UIColor whiteColor]
}];
self.searchField.attributedPlaceholder = placeholder;
self.searchField.textColor = [UIColor whiteColor];
self.searchField.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
self.searchField.layer.cornerRadius = 15;
self.searchField.leftView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"magnifyingglass"]];
self.searchField.leftViewMode = UITextFieldViewModeAlways;
self.searchField.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.searchField];
self.flagIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
self.flagIcon.contentMode = UIViewContentModeScaleAspectFit;
self.flagIcon.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.flagIcon];
UICollectionViewFlowLayout *tabLayout = [[UICollectionViewFlowLayout alloc] init];
tabLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 横向滚动
tabLayout.itemSize = CGSizeMake(100, 40); // 每个标签宽100,高40
tabLayout.minimumInteritemSpacing = 0;
self.countryTabBar = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:tabLayout];
self.countryTabBar.backgroundColor = [UIColor blackColor];
self.countryTabBar.delegate = self;
self.countryTabBar.dataSource = self;
[self.countryTabBar registerClass:[UICollectionViewCell class]// 单元格复用类
forCellWithReuseIdentifier:@"CountryTabCell"];
self.countryTabBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.countryTabBar];
// 5. 内容容器,这个承接子视图
self.contentContainer = [[UIView alloc] init];
self.contentContainer.backgroundColor = [UIColor blackColor];
self.contentContainer.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.contentContainer];
NSArray *tabItems = @[
@{@"title":@"首页", @"icon":@"house.fill", @"selected":@NO},
@{@"title":@"行情", @"icon":@"chart.line.uptrend.xyaxis", @"selected":@YES},
@{@"title":@"自选", @"icon":@"plus", @"selected":@NO},
@{@"title":@"我的", @"icon":@"person", @"selected":@NO}
];
self.commonTabBar = [[CommonTabBar alloc] initWithTabItems:tabItems];
self.commonTabBar.delegate = self;
self.commonTabBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.commonTabBar];
}
#pragma mark - 布局约束
- (void)setupConstraints {
// Logo
[NSLayoutConstraint activateConstraints:@[
[self.logoImage.centerYAnchor constraintEqualToAnchor:self.searchField.centerYAnchor],
[self.logoImage.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15],
[self.logoImage.widthAnchor constraintEqualToConstant:40],
[self.logoImage.heightAnchor constraintEqualToConstant:40]
]];
// 搜索框
[NSLayoutConstraint activateConstraints:@[
[self.searchField.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:50],
[self.searchField.leadingAnchor constraintEqualToAnchor:self.logoImage.trailingAnchor constant:10],
[self.searchField.trailingAnchor constraintEqualToAnchor:self.flagIcon.leadingAnchor constant:-10],
[self.searchField.heightAnchor constraintEqualToConstant:30]
]];
// 国旗
[NSLayoutConstraint activateConstraints:@[
[self.flagIcon.centerYAnchor constraintEqualToAnchor:self.searchField.centerYAnchor],
[self.flagIcon.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-15],
[self.flagIcon.widthAnchor constraintEqualToConstant:40],
[self.flagIcon.heightAnchor constraintEqualToConstant:40]
]];
// 国家
[NSLayoutConstraint activateConstraints:@[
[self.countryTabBar.topAnchor constraintEqualToAnchor:self.searchField.bottomAnchor constant:15],
[self.countryTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.countryTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
[self.countryTabBar.heightAnchor constraintEqualToConstant:40]
]];
// 子视图容器
[NSLayoutConstraint activateConstraints:@[
[self.contentContainer.topAnchor constraintEqualToAnchor:self.countryTabBar.bottomAnchor],
[self.contentContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.contentContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
[self.contentContainer.bottomAnchor constraintEqualToAnchor:self.commonTabBar.topAnchor]
]];
// 底部TabBar
[NSLayoutConstraint activateConstraints:@[
[self.commonTabBar.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
[self.commonTabBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.commonTabBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
]];
}
#pragma mark - 切换子控制器
- (void)switchToCountryAtIndex:(NSInteger)index {
// 先移除
if (self.currentChildVC) {
[self.currentChildVC willMoveToParentViewController:nil];
[self.currentChildVC.view removeFromSuperview];
[self.currentChildVC removeFromParentViewController];
self.currentChildVC = nil;
}
// 创建新的
CountryModel *selectedCountry = self.countries[index];
Class childVCClass = NSClassFromString(selectedCountry.vcClassName);// 字符串转Class????
UIViewController *childVC = [[childVCClass alloc] init];
// 添加
[self addChildViewController:childVC];
childVC.view.frame = self.contentContainer.bounds;// 适配容器
childVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;// 自适应容器大小
[self.contentContainer addSubview:childVC.view];
[childVC didMoveToParentViewController:self];
self.currentChildVC = childVC;
}
#pragma mark - 国家数据源
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.countries.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CountryTabCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
// 国家名称
UILabel *nameLabel = [cell viewWithTag:100];
if (!nameLabel) {
nameLabel = [[UILabel alloc] init];
nameLabel.tag = 100;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.font = [UIFont systemFontOfSize:15];
nameLabel.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:nameLabel];
nameLabel.frame = cell.contentView.bounds;// 撑满单元格
}
nameLabel.text = self.countries[indexPath.item].name;
// 选中下划线
UIView *selectedLine = [cell viewWithTag:101];
if (!selectedLine) {
selectedLine = [[UIView alloc] init];
selectedLine.tag = 101;
selectedLine.backgroundColor = [UIColor blueColor];
[cell.contentView addSubview:selectedLine];
selectedLine.frame = CGRectMake(30, cell.contentView.bounds.size.height - 7, 40, 4);
}
selectedLine.hidden = !(indexPath.item == self.selectedIndex);
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndex = indexPath.item;
[self.countryTabBar reloadData]; // 刷新标签选中状态
[self switchToCountryAtIndex:indexPath.item]; // 切换到对应国家页面
}
#pragma mark - 底部跳转方法
- (void)tabBarDidSelectIndex:(NSInteger)index {
switch (index) {
case 0:
[self pushHome];
break;
case 1:
break;
case 2:
break;
case 3:
[self pushMine];
break;
}
}
- (void) pushMine {
MyViewController *myViewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
}
- (void) pushHome {
HomeViewController *myViewController = [[HomeViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
}
@end