【发布时间】:2023-03-10 04:09:01
【问题描述】:
在我的导航栏中,我有一个放大镜图标,它会显示一个搜索栏。我没有使用 UISearchDisplayController,所以我选择构建自己的 UINavigationItem,然后使用 pushNavigationItem 将其推送到标准 UINavigationItem。
问题在于 UINavigationItem 似乎被推到了右侧大约 8 个像素。这会导致取消按钮(带有本地化文本“Annuleren”)非常靠近屏幕边缘。
我尝试在运行时检查 self.mySearchBar.bounds,但原点是 0,0。我玩过一些 AutoLayout 并以编程方式添加约束,但我没有成功。我希望没有 AutoLayout 也是可能的。
这是我的代码:
- (IBAction)displaySearchBar:(id)sender {
if (!self.mySearchNavigationItem)
{
self.mySearchNavigationItem = [[UINavigationItem alloc] initWithTitle:@""];
self.mySearchNavigationItem.hidesBackButton = YES;
self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.mySearchBar.showsCancelButton = YES;
self.mySearchBar.delegate = self;
[self.mySearchBar sizeToFit];
[self.mySearchBar setPlaceholder:@"Zoeken..."];
UIView *barWrapper = [[UIView alloc]initWithFrame:self.mySearchBar.bounds];
[barWrapper addSubview:self.mySearchBar];
self.mySearchNavigationItem.leftBarButtonItem = nil;
self.mySearchNavigationItem.backBarButtonItem = nil;
self.mySearchNavigationItem.titleView = barWrapper;
UIButton *cancelButton;
UIView *topView = self.mySearchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
[cancelButton setTitle:@"Annuleren" forState:UIControlStateNormal];
}
}
[self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];
NSTimeInterval delay;
if (self.tableView.contentOffset.y >1000) delay = 0.4;
else delay = 0.1;
[self performSelector:@selector(activateSearch) withObject:nil afterDelay:delay];
}
【问题讨论】:
标签: objective-c ios7 uinavigationbar uisearchbar uinavigationitem