3
This commit is contained in:
@@ -2,11 +2,6 @@
|
||||
// KBSearchResultVC.m
|
||||
// keyBoard
|
||||
//
|
||||
// 搜索结果页:顶部为 KBSearchBarView,下面为两列卡片的 UICollectionView。
|
||||
// - Masonry 约束
|
||||
// - 懒加载子视图
|
||||
// - 中文注释
|
||||
//
|
||||
|
||||
#import "KBSearchResultVC.h"
|
||||
#import "KBSearchBarView.h"
|
||||
@@ -16,7 +11,9 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
|
||||
|
||||
@interface KBSearchResultVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
|
||||
// 顶部搜索栏(复用已封装的 KBSearchBarView)
|
||||
// 顶部自定义栏:返回按钮 + 搜索栏
|
||||
@property (nonatomic, strong) UIView *topBar;
|
||||
@property (nonatomic, strong) UIButton *backButton;
|
||||
@property (nonatomic, strong) KBSearchBarView *searchBarView;
|
||||
|
||||
// 结果列表
|
||||
@@ -35,20 +32,32 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
|
||||
self.view.backgroundColor = [UIColor whiteColor];
|
||||
|
||||
// 添加子视图
|
||||
[self.view addSubview:self.searchBarView];
|
||||
[self.view addSubview:self.topBar];
|
||||
[self.topBar addSubview:self.backButton];
|
||||
[self.topBar addSubview:self.searchBarView];
|
||||
[self.view addSubview:self.collectionView];
|
||||
|
||||
// Masonry 布局:搜索在顶部,列表紧随其下
|
||||
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
// 顶部与导航栏底对齐,左右各 16 间距,高度 40
|
||||
make.top.equalTo(self.view.mas_top).offset(KB_NAV_TOTAL_HEIGHT + 8);
|
||||
make.left.equalTo(self.view).offset(16);
|
||||
make.right.equalTo(self.view).offset(-16);
|
||||
make.height.mas_equalTo(40);
|
||||
// Masonry 布局:顶部自定义栏(无导航栏)
|
||||
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.view.mas_top).offset(KB_STATUSBAR_HEIGHT + 8);
|
||||
make.left.right.equalTo(self.view);
|
||||
make.height.mas_equalTo(44);
|
||||
}];
|
||||
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.topBar).offset(12);
|
||||
make.centerY.equalTo(self.topBar);
|
||||
make.width.mas_equalTo(28);
|
||||
make.height.mas_equalTo(36);
|
||||
}];
|
||||
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.centerY.equalTo(self.backButton);
|
||||
make.left.equalTo(self.backButton.mas_right).offset(12);
|
||||
make.width.mas_equalTo(315);
|
||||
make.height.mas_equalTo(36);
|
||||
make.right.lessThanOrEqualTo(self.topBar).offset(-16);
|
||||
}];
|
||||
|
||||
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.searchBarView.mas_bottom).offset(12);
|
||||
make.top.equalTo(self.topBar.mas_bottom).offset(12);
|
||||
make.left.right.bottom.equalTo(self.view);
|
||||
}];
|
||||
|
||||
@@ -62,6 +71,19 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated {
|
||||
[super viewWillAppear:animated];
|
||||
// 隐藏系统导航栏
|
||||
[self.navigationController setNavigationBarHidden:YES animated:animated];
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated {
|
||||
[super viewWillDisappear:animated];
|
||||
if (self.isMovingFromParentViewController || self.isBeingDismissed) {
|
||||
[self.navigationController setNavigationBarHidden:NO animated:animated];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
/// 执行搜索(示例:本地生成一些数据)
|
||||
@@ -138,6 +160,35 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
|
||||
return _searchBarView;
|
||||
}
|
||||
|
||||
- (UIView *)topBar {
|
||||
if (!_topBar) {
|
||||
_topBar = [[UIView alloc] init];
|
||||
_topBar.backgroundColor = [UIColor whiteColor];
|
||||
}
|
||||
return _topBar;
|
||||
}
|
||||
|
||||
- (UIButton *)backButton {
|
||||
if (!_backButton) {
|
||||
_backButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
UIImage *img = nil;
|
||||
if (@available(iOS 13.0, *)) {
|
||||
img = [UIImage systemImageNamed:@"chevron.left"];
|
||||
}
|
||||
if (img) {
|
||||
[_backButton setImage:img forState:UIControlStateNormal];
|
||||
} else {
|
||||
[_backButton setTitle:@"<" forState:UIControlStateNormal];
|
||||
_backButton.titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
|
||||
}
|
||||
[_backButton setTintColor:[UIColor blackColor]];
|
||||
[_backButton addTarget:self action:@selector(onTapBack) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _backButton;
|
||||
}
|
||||
|
||||
- (void)onTapBack { [self.navigationController popViewControllerAnimated:YES]; }
|
||||
|
||||
- (UICollectionViewFlowLayout *)flowLayout {
|
||||
if (!_flowLayout) {
|
||||
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
|
||||
@@ -166,4 +217,3 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user