0

I'm searching for a way to add a UILabel to an UISearchBar in Front of the Searchfield.

I tried to add a UILabel like that:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
  self.searchDisplayController.searchBar.showsCancelButton = YES;

  UILabel *searchResultCount = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 50, 28)];
  searchResultCount.text = @"357"; //Itemscount of Searchresult

  [self.searchDisplayController.searchBar addSubview:searchResultCount];
}

The Result looks like that ==> https://i.stack.imgur.com/x3sel.png

How can i get this work ? It should be

[356] [Searchfield] [Cancel]

Thanks for any advise

blub
  • 359
  • 6
  • 23
  • do you make use of the clear button on the search bar ? (clear button - is the X button, that clears all the text when pressed) – Basheer_CAD Feb 10 '14 at 17:26
  • @Basheer_CAD: yes i need the clear button. it needs to be a additional UILabel Control in front of the Searchbar-Textfield. – blub Feb 10 '14 at 18:01

2 Answers2

1

You can adjust search text position. Just add:

self.searchDisplayController.searchBar.searchTextPositionAdjustment = UIOffsetMake(65., 0.);
graver
  • 15,183
  • 4
  • 46
  • 62
  • searchFieldBackgroundPositionAdjustment looks better for me. in detail like that self.searchDisplayController.searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(42, 0); BUT now it cut's me the Cancel-Button Title away... see here => http://i.stack.imgur.com/hIjbq.png How can i solve that ? I simple want to minimize the Width of the Searchfield. How can i do that ? – blub Feb 11 '14 at 07:52
  • Your search bar's width is higher than the screen's width – graver Feb 11 '14 at 08:22
  • Thx Graver, but how can fix this Issue and set the width of the Searchbar programmatically ? Thx again – blub Feb 11 '14 at 14:09
0

I did solved it. Subclass UISearchBar and make changes in layoutSubviews.

Here is my Code.

#import "CustomSearchBar.h"

@implementation CustomSearchBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)layoutSubviews
{
  [super layoutSubviews];

  float cancelButtonWidth = 84.0;
  UITextField *searchField;
  UILabel *resultLabel;

  UIView *topView = [self.subviews lastObject];
  for (UIView *subView in topView.subviews)
  {
    if ([subView isKindOfClass:NSClassFromString(@"UITextField")])
    {
      searchField = (UITextField *)subView;
    }
    else if ([subView isKindOfClass:NSClassFromString(@"UILabel")])
    {
      resultLabel = (UILabel*)subView;
    }
  }

  if(!searchField) {return; }

  if (self.showsCancelButton)
  {
    if(!resultLabel)
    {
      resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 40, 28)];
      resultLabel.text = @"356";
      [topView addSubview:resultLabel];
    }
    [searchField setFrame:CGRectMake(60, 8, self.frame.size.width - 100 - cancelButtonWidth, 28)];

  }
  else
  {
    [searchField setFrame:CGRectMake(8, 8, 484, 28)];
    if(resultLabel)
    {
      [resultLabel removeFromSuperview];
    }
  }
}
@end

Cheers

blub
  • 359
  • 6
  • 23