Uitableview UIToolbar中的iOS8 UISearchBar没有剩余填充
我有一个包含UITableView的UIViewController类。在表视图标题中,我有一个UIToolbar,其中包括一个UISearchBar。在iOS8中,当我点击搜索栏进行搜索时,搜索显示控制器会按预期将搜索栏设置为屏幕顶部的动画,但搜索栏的左侧没有边距 最精简的代码复制如下:Uitableview UIToolbar中的iOS8 UISearchBar没有剩余填充,uitableview,ios8,uisearchbar,uitoolbar,Uitableview,Ios8,Uisearchbar,Uitoolbar,我有一个包含UITableView的UIViewController类。在表视图标题中,我有一个UIToolbar,其中包括一个UISearchBar。在iOS8中,当我点击搜索栏进行搜索时,搜索显示控制器会按预期将搜索栏设置为屏幕顶部的动画,但搜索栏的左侧没有边距 最精简的代码复制如下: - (void)viewDidLoad { [super viewDidLoad]; UIToolbar *toolbar = [[UIToolbar alloc] initWithFram
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, [self tableView].frame.size.width, 44.0)];
if ([toolbar respondsToSelector:@selector(setBarTintColor:)]) {
[toolbar setBarTintColor:[UIColor lightGrayColor]];
}
[[self tableView] setTableHeaderView:toolbar];
UIView *searchBarView = [[UIView alloc] initWithFrame:[[[self searchDisplayController] searchBar] frame]];
[[[self searchDisplayController] searchBar] setBackgroundImage:[[UIImage alloc] init]];
[searchBarView addSubview:[[self searchDisplayController] searchBar]];
[[[self searchDisplayController] searchBar] setText:@""];
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarView];
[toolbar setItems:@[searchBarItem]];
}
非常感谢您的帮助/建议
编辑:这在iOS 6.1和7.0/1上正常工作。这似乎是一个iOS 8错误。
您可以使用来自的临时解决方案
基本上,您可以创建UIToolbar的子类。
然后在刚刚创建的子类中,添加以下代码以追加缺少的空格:
#define DEFAULT_APPLE_PADDING 20.0f
-(void)layoutSubviews{
[super layoutSubviews];
[self.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
if ([NSStringFromClass(view.class) hasPrefix:@"UIToolbar"] &&
[NSStringFromClass(view.class) hasSuffix:@"Button"]) {
CGRect buttonFrame = view.frame;
if (buttonFrame.origin.x == 0) {
buttonFrame.origin.x = DEFAULT_APPLE_PADDING;
} else if (buttonFrame.origin.x + buttonFrame.size.width == self.bounds.size.width) {
buttonFrame.origin.x -= DEFAULT_APPLE_PADDING;
}
view.frame = buttonFrame;
}
}];
}
如果这些链接断开,仅引用外部链接的答案将过时。请提供一些说明和代码,说明链接中的内容如何帮助回答OP的问题。