This commit is contained in:
2025-11-08 20:49:05 +08:00
parent 3b0beb52da
commit a729396401
2 changed files with 135 additions and 44 deletions

View File

@@ -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

View File

@@ -27,9 +27,10 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
};
@interface KBSearchVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
// View titleView Y
// +
@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) KBSearchBarView *searchBarView;
@property (nonatomic, strong) UIView *titleContainer; // searchBarView
//
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewLeftAlignedLayout *flowLayout;
@@ -48,14 +49,35 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
self.view.backgroundColor = [UIColor whiteColor];
// UI
[self.view addSubview:self.topBar];
[self.topBar addSubview:self.backButton];
[self.topBar addSubview:self.searchBarView];
[self.view addSubview:self.collectionView];
// titleViewY
self.navigationItem.titleView = self.titleContainer;
// - 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);
}];
// - Masonry
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(KB_NAV_TOTAL_HEIGHT + 8);
make.top.equalTo(self.topBar.mas_bottom).offset(12);
make.left.right.bottom.equalTo(self.view);
}];
@@ -82,6 +104,20 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 使
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// push
if (self.isMovingFromParentViewController || self.isBeingDismissed) {
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
}
#pragma mark - Private
/// /
@@ -337,30 +373,35 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
return _searchBarView;
}
- (UIView *)titleContainer {
if (!_titleContainer) {
// 315 36 Y titleView
CGFloat width = 315.0;
_titleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 36.0)];
_titleContainer.backgroundColor = [UIColor clearColor];
// 使 AutoLayout 315x36
_titleContainer.translatesAutoresizingMaskIntoConstraints = NO;
[[_titleContainer.widthAnchor constraintEqualToConstant:width] setActive:YES];
[[_titleContainer.heightAnchor constraintEqualToConstant:36.0] setActive:YES];
[_titleContainer setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_titleContainer setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
// searchBarView
[_titleContainer addSubview:self.searchBarView];
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_titleContainer);
make.height.mas_equalTo(36.0);
}];
- (UIView *)topBar {
if (!_topBar) {
_topBar = [[UIView alloc] init];
_topBar.backgroundColor = [UIColor whiteColor];
}
return _titleContainer;
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]; }
- (UICollectionViewLeftAlignedLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];