【问题标题】:How to get rid of the space on the left side of a custom UINavigationItem with a UISearchBar如何使用搜索栏摆脱自定义导航项左侧的空间
【发布时间】: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


    【解决方案1】:

    尝试:

    self.navigationController.navigationBar.barTintColor = self.mySearchBar.barTintColor;
    

    如果这不起作用,您可以在导航控制器中添加您想要的颜色的底层视图。这可能有用:Get the right color in iOS7 translucent navigation bar

    【讨论】:

    • 感谢您的帮助。它不会改变任何东西,但我明白你想要达到的目标。使用 self.mySearchBar.searchBarStyle = UISearchBarStyleMinimal;或多或少也会这样做,它会删除灰色背景。问题是我的取消按钮('Annuleren')仍然非常靠近屏幕的右边缘。我希望找到一个解决方案,消除屏幕左侧和搜索栏之间的 8 像素空间。
    • 啊,我还有一个关于我过去为摆脱多余空间所做的事情的建议。您可以在导航栏上添加 UIView,并在 UIView 中添加搜索栏和按钮。如果需要,我可以稍后发布示例。
    【解决方案2】:

    在搜索了几个小时后,我放弃了并进行了肮脏的修复。我会暂时打开它,以防有人知道为什么我的搜索栏向右移动了 8 个像素。

    在显示 UINavigationItem 之前,我将整个 UINavigationBar 移动到 x 坐标 -8。

    self.navigationController.navigationBar.frame = CGRectMake(-8.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
    [self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];
    

    然后单击取消按钮,我将其移回 x 坐标 0。

    - (IBAction)cancelSearchBar:(id)sender {
        [self.navigationController.navigationBar popNavigationItemAnimated:YES];    
        self.navigationController.navigationBar.frame = CGRectMake(0.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 2021-02-07
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多