问题描述
我正在尝试使用其中的TextField完成我的UISearchBar的相同外观,就像我的iOS 6应用程序一样。我曾试图用几种方式对它进行编码,但尚未成功。问题是,自iOS 7以来,我无法以任何方式更改TextField的框架。结果是,我的TextField获取NavigationBar中的所有空间并覆盖右侧的UIBarButtonItem(菜单按钮)。见下图:
I am trying to accomplish the same look of my UISearchBar with a TextField within it, as in my iOS 6 app. I have tried to code it in several ways and not yet been successful. The problem is, I am not able to change the TextField's frame in any way since iOS 7. The result is, my TextField takes all the space in the NavigationBar and overrides the UIBarButtonItem (menu button) to the right. See pictures below:
iOS 6代码: 这是我在iOS 6中编码的方式,我可以将TextFields框架设置为我喜欢的任何内容!
UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject];
[sbTextField removeFromSuperview];
CGRect rect = searchBar.frame;
rect.size.height = 32;
rect.size.width = 210;
sbTextField.frame = rect;
[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
[[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];
iOS 7中上述代码的结果:![iOS 7看看]
The result from the code above in iOS 7: ![iOS 7 look]
iOS 7代码: iOS 7的不同之处在于您需要使用subViews,以便将UITextField添加到UISearchBar / UINavigationBar。通过这样做,我还没有能够改变它的框架。它目前与右边的menuButton重叠,可以在下面的代码中看到...
UITextField* sbTextField;
CGRect rect = subView.frame;
rect.size.height = 32;
rect.size.width = 115;
for (UIView *subView in self.searchBar.subviews){
for (UIView *ndLeveSubView in subView.subviews){
if ([ndLeveSubView isKindOfClass:[UITextField class]])
{
sbTextField = (UITextField *)ndLeveSubView;
sbTextField.backgroundColor =[UIColor whiteColor];
UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
sbTextField.frame = rect;
self.navigationItem.leftBarButtonItem = searchBarNavigationItem;
self.navigationItem.rightBarButtonItem = menuButton;
[sbTextField removeFromSuperview];
break;
}
}
}
[self.searchBar reloadInputViews];
SO ...是否可以以任何方式更改子视图的框架(TextField)? :(
SO...Is it possible to change a subView's frame (TextField) in any way ? :(
编辑
答案有点蹩脚。为了使代码在ios7中使用TextField右侧的按钮,TextField必须设置为navigationBar的titleView。在ios 6中不是这种情况。但是会有其他故障并且不建议在iOS7中的searchBars中使用TextField。请使用searchDispalyController。请在下面找到我的答案
self.navigationItem.titleView = sbTextField;
推荐答案
Thanx to spotdog13。我终于成功了它可以通过以下方式正确地适用于iOS 7:
Thanx to spotdog13. I finally managed to make it work for iOS 7 properly in the following way:
#define TABLE_BOTTOM_MARGIN 5
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize searchBar;
@synthesize searchResults;
- (void)viewDidLoad
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[searchBar sizeToFit]; // standard size
searchBar.delegate = self;
// Add search bar to navigation bar
self.navigationItem.titleView = searchBar;
}
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Manually activate search mode
// Use animated=NO so we'll be able to immediately un-hide it again
[self.searchDisplayController setActive:YES animated:NO];
// Hand over control to UISearchDisplayController during the search
// searchBar.delegate = (id <UISearchBarDelegate>)self.searchDisplayController;
return YES;
}
#pragma mark <UISearchDisplayDelegate>
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController
// Un-hide the navigation bar that UISearchDisplayController hid
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController
*)controller {
searchBar = (UISearchBar *)self.navigationItem.titleView;
// Manually resign search mode
[searchBar resignFirstResponder];
// Take back control of the search bar
searchBar.delegate = self;
}
这篇关于iOS 7中UISearchBar中的UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!