浅谈Xcode9 和iOS11适配和特性
- (void)resetSearchBar
{
CGFloat leftButtonWidth = 35, rightButtonWidth = 75; // left padding right padding
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - leftButtonWidth - rightButtonWidth, 44)];
self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
[container addSubview:self.searchBar];
CGFloat offset = (rightButtonWidth - leftButtonWidth) / 2;
// 给searchBar添加约束
[NSLayoutConstraint activateConstraints:@[
[self.searchBar.topAnchor constraintEqualToAnchor:container.topAnchor], // 顶部约束
[self.searchBar.leftAnchor constraintEqualToAnchor:container.leftAnchor constant:-25*ScreenScaleX], // 左边距约束
[self.searchBar.rightAnchor constraintEqualToAnchor:container.rightAnchor constant:0], // 右边距约束
[self.searchBar.bottomAnchor constraintEqualToAnchor:container.bottomAnchor], // 底部约束
[self.searchBar.centerXAnchor constraintEqualToAnchor:container.centerXAnchor constant:-offset], // 横向中心约束
// [self.searchBar.widthAnchor constraintEqualToAnchor:container.widthAnchor constant:width] // 宽度约束
]];
self.navigationItem.titleView = container; // 顶部导航搜索
}