以下是如何使取消按钮和范围栏仅在编辑时显示。
带有动画取消按钮(取消)外观动画的完整代码:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBar.showsScopeBar = YES;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:YES animated:YES];
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
searchBar.showsScopeBar = NO;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
编辑 - 斯威夫特版本 3
public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsScopeBar = true
searchBar.sizeToFit()
searchBar.setShowsCancelButton(true, animated: true)
return true
}
public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsScopeBar = false
searchBar.sizeToFit()
searchBar.setShowsCancelButton(false, animated: true)
return true
}
资料来源:http ://www.alexandre-gomes.com/?p=418