From 2b1829cd68e51a3a073fbf3f4ecb315c85ae8a2e Mon Sep 17 00:00:00 2001 From: iphone5solo <499491531@qq.com> Date: Mon, 9 Jan 2017 10:18:58 +0800 Subject: [PATCH] Fixed #41 searchBar layout issues Fixed #41 searchBar layout issues --- PYSearch/PYSearchViewController.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/PYSearch/PYSearchViewController.m b/PYSearch/PYSearchViewController.m index 9e065cb..b10257d 100644 --- a/PYSearch/PYSearchViewController.m +++ b/PYSearch/PYSearchViewController.m @@ -220,6 +220,7 @@ - (void)viewWillDisappear:(BOOL)animated [self.searchBar resignFirstResponder]; } + /** 控制器销毁 */ - (void)dealloc { @@ -266,13 +267,23 @@ - (void)setup titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2; titleView.py_height = 30; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds]; - searchBar.py_width -= PYSEARCH_MARGIN * 1.5; + [titleView addSubview:searchBar]; + self.navigationItem.titleView = titleView; + // 关闭自动调整 + searchBar.translatesAutoresizingMaskIntoConstraints = NO; + // 为titleView添加约束来调整搜索框 + NSLayoutConstraint *widthCons = [NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]; + NSLayoutConstraint *heightCons = [NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]; + NSLayoutConstraint *xCons = [NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; + NSLayoutConstraint *yCons = [NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; + [titleView addConstraint:widthCons]; + [titleView addConstraint:heightCons]; + [titleView addConstraint:xCons]; + [titleView addConstraint:yCons]; searchBar.placeholder = [NSBundle py_localizedStringForKey:PYSearchSearchPlaceholderText]; searchBar.backgroundImage = [UIImage imageNamed:@"clearImage" inBundle:[NSBundle py_searchBundle] compatibleWithTraitCollection:nil]; searchBar.delegate = self; - [titleView addSubview:searchBar]; self.searchBar = searchBar; - self.navigationItem.titleView = titleView; // 设置头部(热门搜索) UIView *headerView = [[UIView alloc] init];