diff --git a/keyBoard/Class/Search/V/KBSearchBarView.m b/keyBoard/Class/Search/V/KBSearchBarView.m index 3d9baf7..92256c0 100644 --- a/keyBoard/Class/Search/V/KBSearchBarView.m +++ b/keyBoard/Class/Search/V/KBSearchBarView.m @@ -30,8 +30,7 @@ // layout - Masonry [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { - make.edges.equalTo(self); - make.height.mas_equalTo(40); + make.edges.equalTo(self); // 高度由外部容器决定 }]; [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) { @@ -47,6 +46,9 @@ make.right.equalTo(self.searchButton.mas_left).offset(-8); make.height.mas_equalTo(30); }]; + + self.bgView.layer.borderWidth = 1; + self.bgView.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor; } #pragma mark - Action @@ -121,4 +123,3 @@ } @end - diff --git a/keyBoard/Class/Search/VC/KBSearchVC.m b/keyBoard/Class/Search/VC/KBSearchVC.m index 54dabb5..1369294 100644 --- a/keyBoard/Class/Search/VC/KBSearchVC.m +++ b/keyBoard/Class/Search/VC/KBSearchVC.m @@ -21,8 +21,9 @@ typedef NS_ENUM(NSInteger, KBSearchSection) { }; @interface KBSearchVC () -// 顶部搜索栏(封装的 View) +// 顶部搜索栏(封装的 View)——放到导航栏的 titleView 中,Y 轴与返回按钮一致 @property (nonatomic, strong) KBSearchBarView *searchBarView; +@property (nonatomic, strong) UIView *titleContainer; // 承载 searchBarView 的容器 // 列表 @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout; @@ -39,19 +40,14 @@ typedef NS_ENUM(NSInteger, KBSearchSection) { self.view.backgroundColor = [UIColor whiteColor]; // UI - [self.view addSubview:self.searchBarView]; [self.view addSubview:self.collectionView]; - // 布局 - Masonry - [self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) { - 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); - }]; + // 将搜索栏放进导航栏 titleView,Y 轴自然与返回按钮对齐 + self.navigationItem.titleView = self.titleContainer; + // 布局 - Masonry(列表顶端紧贴导航栏底部) [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { - make.top.equalTo(self.searchBarView.mas_bottom).offset(12); + make.top.equalTo(self.view.mas_top).offset(KB_NAV_TOTAL_HEIGHT + 8); make.left.right.bottom.equalTo(self.view); }]; @@ -192,6 +188,30 @@ typedef NS_ENUM(NSInteger, KBSearchSection) { return _searchBarView; } +- (UIView *)titleContainer { + if (!_titleContainer) { + // 固定尺寸:宽 315,高 36(与返回按钮同一 Y 轴,作为 titleView 显示) + CGFloat width = KBFit(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); + }]; + + } + return _titleContainer; +} + - (UICollectionViewFlowLayout *)flowLayout { if (!_flowLayout) { _flowLayout = [[UICollectionViewFlowLayout alloc] init];