2010-08-14 89 views
26

我正在撕掉我的头发。我的客户希望一个按钮添加到如下面的例子搜索栏的左边:将按钮添加到UISearchBar的左边

alt text http://www.erik.co.uk/images/IMG_0008.PNG

但我就是无法弄清楚如何做到这一点。 Apple似乎没有提供任何记录的方法来将自定义按钮添加到UISearchBar,更不用说在搜索栏的左侧。

我试过在界面生成器中添加一个UIToolbar,左边有一个按钮,但是我找不到任何样式的组合,在这两个列表中正确地给出了它们是一个的印象。总是有什么样子垂直取向的一个像素的差异,你可以从下面的图片中看到:

alt text http://www.erik.co.uk/images/verticalalignment.png

我已搜索周围,只是无法找到答案,但我们可以看到从截图一定是可能的!

非常感谢您的帮助。

Erik

+0

的权利雨燕3.0的版本:[发布7203924(https://stackoverflow.com/a/44621016/7203924) – 2017-06-19 02:03:43

回答

39

使用导航栏而不是工具栏。将搜索栏设置为导航栏的标题视图。

Interface Builder

screenshot 1

结果:

screenshot 2

+0

感谢KennyTM,一个完美的答案!正是我想要的! – 2010-08-14 12:57:48

+0

@Erik Carlson ...我也在寻找将按钮添加到searchBar中...... – 2010-12-16 05:04:06

+0

你的照片已经死了。你可以吃吗? – Will 2011-01-17 14:19:15

4

最简单的解决办法是增加你的搜索条在工具栏的顶部,(不),我给你的我在公司eBuildy中使用的最佳解决方案:

UIBarButtonItem *mySettingsButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(refresh)]; 
UIBarButtonItem *mySpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem *myRefreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)]; 
UIToolbar *myTopToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,320,40)]; 
UISearchBar *mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(70,1,220,40)]; 

[myTopToolbar setItems:[NSArray arrayWithObjects:mySettingsButton,mySpacer,myRefreshButton, nil] animated:NO]; 
[self.view addSubview:myTopToolbar]; 
[self.view addSubview:mySearchBar]; 
8

第一个解决方案是使用UINavigationBar而不是UIToolbar,正如KennyTM注意到的那样。但是当我需要使用3个按钮(导航栏只允许使用2个按钮)时,您可能对导航栏感到不满意 - 请参阅左侧图片。这是我做的:

  1. 使用Toolbar有3个按钮,并在搜索栏应该放置的地方Flexible Space Bar Button Item
  2. 将搜索栏放在(不在)的工具栏上。要在Interface Builder中这样做,请不要将&拖放到工具栏上的搜索栏中。相反,将它放在附近的某个地方,然后使用键盘上的箭头键将其移动到位置(或通过更改Interface Builder中的X & Y位置)。
  3. 搜索栏左下方的黑线(请参阅右图)。为了隐藏它,我放置了一个高度为1px的附加视图和一个白色背景。

对我来说这看起来有点脏,所以如果你有更好的解决方案,让我知道。

screenshot

+0

+1简单和干净。将您的按钮和搜索栏嵌入到UIToolbar中。 – Marco 2012-01-20 19:27:06

3

回答在座的一个老问题,但我用这一个我最近挣扎,发现了一些不足之处与其他答案的情况下,我试图解决的问题。这里是我在UISearchBar的子类中做的:

首先添加一个UIButton属性(这里是“selectButton”)。然后覆盖initWithFrame方法和做类似下面的内容:

-(id)initWithFrame:(CGRect)frame{ 
if (self = [super initWithFrame:frame]) 
{ 
    self.selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    self.selectButton.contentEdgeInsets = (UIEdgeInsets){.left=4,.right=4}; 
    [self.selectButton addTarget:self action:@selector(pressedButton:) forControlEvents:UIControlEventTouchUpInside]; 

    self.selectButton.titleLabel.numberOfLines = 1; 
    self.selectButton.titleLabel.adjustsFontSizeToFitWidth = YES; 
    self.selectButton.titleLabel.lineBreakMode = UILineBreakModeClip; 

    [self addSubview:self.selectButton]; 
    [self.selectButton setFrame:CGRectMake(5, 6, 60, 31)]; 
} 

return self; 
} 

现在要覆盖布局子视图的方法来搜索栏调整到合适的宽度,这取决于取消按钮是否被显示。应该看起来像这样:

-(void)layoutSubviews 
{ 
[super layoutSubviews]; 

float cancelButtonWidth = 65.0; 
UITextField *searchField = [self.subviews objectAtIndex:1]; 

if (self.showsCancelButton == YES) 
    [searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70 - cancelButtonWidth, 31)]; 
else 
    [searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70, 31)]; 
} 

请注意,在上面的方法中,我为cancelButtonWidth添加了一个常量。我试图添加代码来获取[self cancelButton]的宽度,但似乎只能在运行时访问,并且不允许项目编译。在任何情况下,这应该是您需要的良好开端

16

您可以替换书签图像,并根据需要调整它的偏移量。
例如:

[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"plus2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; 
[self.searchDisplayController.searchBar setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconBookmark]; 

处理的委托方法的按钮事件:

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar

这是它的外观:

​​3210

0

如果你想有一个自定义按钮在右侧,取代Cancel按钮,只要使用这个代码(有效的iOS 9及以上):

[self.searchBar setShowsCancelButton:YES]; 
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setTitle:@""]; 
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setImage:[UIImage imageNamed:@"search"]]; 
+0

你会如何覆盖搜索栏取消方法? – Matt 2017-09-30 20:08:39

+0

请看看这个答案:https://stackoverflow.com/questions/13799074/how-to-override-the-cancel-button-in-the-uisearchbar – Winston 2017-10-06 15:22:15

0

与来自先前答案一些启示我已发现为iOS 9和上面的清洁方法,包括:在水平堆叠只是地方搜索栏鉴于,让栈做它的魔力,可以再加入尽可能多的按钮,你喜欢向左或容易

UIStackView *searchStack = [[UIStackView alloc] initWithFrame:searchBar.frame]; 
    searchStack.axis = UILayoutConstraintAxisHorizontal; 
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [searchStack addArrangedSubview:leftButton]; 
    [searchStack addArrangedSubview:searchBar]; 
    self.tableView.tableHeaderView = searchStack;