IOS 15设置UISearchBar 未编辑居中,编辑居左
原理很简单, 不编辑的时候设置偏移量居中,编辑的时候置0
[searchBar.searchTextField addTarget:self action:@selector(BeginEdit:) forControlEvents:UIControlEventEditingDidBegin];
[searchBar.searchTextField addTarget:self action:@selector(EndEdit:) forControlEvents:UIControlEventEditingDidEnd];
-(void)BeginEdit:(UITextField*)textfiled{
[UIView animateWithDuration:1 animations:^{
[self->_searchBar setPositionAdjustment:UIOffsetMake(0, 0) forSearchBarIcon:UISearchBarIconSearch];
}];
}
-(void)EndEdit:(UITextField*)textfiled{
[UIView animateWithDuration:1 animations:^{
[self->_searchBar setPositionAdjustment:UIOffsetMake(ComponentSearchOffset, 0) forSearchBarIcon:UISearchBarIconSearch];
}];
}