ios7 searchBar in navigationBar cover rightbuttonItem - ios

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
searchBar.tintColor=[UIColor redColor];
searchBar.barTintColor=[UIColor clearColor];
searchBar.translucent=NO;
CGRect newFrame = searchBar.frame;
newFrame.size.width = CGRectGetWidth(newFrame)-self.navigationItem.rightBarButtonItem.width;
searchBar.frame = newFrame;
UIView *barWrapper = [[UIView alloc]initWithFrame:searchBar.bounds];
barWrapper.backgroundColor=[UIColor clearColor];
[barWrapper addSubview:searchBar];
self.navigationItem.titleView = barWrapper;
NSLog(#"titleview = %#",self.navigationItem.titleView.frame);
_searchBar=searchBar;
It always cover rightButtonItem.How to set it?
_searchBar want not to change.
self.navigationItem.rightBarButtonItem.width =>0.

Related

iOS 11 UISearchController.searchBar have a blackColor view

I am using the following code for setting up the search controller
_searchController = [[UISearchController alloc]initWithSearchResultsController:self.resultVC];
self.definesPresentationContext = YES;
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.hidesNavigationBarDuringPresentation = YES;
_searchController.searchBar.barTintColor = [UIColor yellowColor];
// 可以通过此种方式可以拿到搜索框,修改搜索框的样式
UITextField *searchField = [_searchController.searchBar valueForKey:#"_searchField"];
searchField.backgroundColor = [UIColor redColor];
searchField.leftView = nil;
searchField.layer.cornerRadius = 4;
searchField.clipsToBounds = YES;
searchField.leftViewMode = UITextFieldViewModeNever;
UIView *backgroundview = searchField.subviews.firstObject;
backgroundview.backgroundColor = [UIColor redColor];
While looking through the view hierarchy, I found a view that I never added
I tried to delete this view but I couldn't because backgroundview doesn't have any subview
NSInteger count = backgroundview.subviews;
for (UIView *sub in backgroundview.subviews) {
NSLog(#"%#", NSStringFromClass(sub.class));// don't have any subView
}
How can I delete this particular view?

Cannot change text and background color of searchbar in navigation bar

I've added a searchbar to my navigation bar and want to change the color of the text and background. I'm using the code below and based on other answers on SO appearanceWhenContainedIn should work but does nothing. What's going wrong here?
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = #"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = NO;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
[searchCon setActive:NO animated:YES];
self.searchDisplayController = searchCon;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.displaysSearchBarInNavigationBar=YES;
The following code helped me fix the issue. I have read methods that set private API may be rejected from apple, and route to go is subView. See below:
if(!itemSearchBar)
{
itemSearchBar = [[UISearchBar alloc] init];
[itemSearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
itemSearchBar.placeholder = #"What are you looking for?";
itemSearchBar.delegate = self;
UITextField* sbTextField;
for (UIView *subView in self.itemSearchBar.subviews){
for (UIView *ndLeveSubView in subView.subviews){
if ([ndLeveSubView isKindOfClass:[UITextField class]])
{
sbTextField = (UITextField *)ndLeveSubView;
//changes typing text color
sbTextField.textColor = [UIColor redColor];
//changes background color
sbTextField.backgroundColor = [UIColor blueColor];
//changes placeholder color
UIColor *color = [UIColor redColor];
sbTextField.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:#"What are you looking for?"
attributes:#{NSForegroundColorAttributeName:color}];
break;
}
}
}
Try following piece of code :
if(!searchBar)
{
searchBar = [[UISearchBar alloc] init];
// searchBar.backgroundColor = [UIColor purpleColor];
// searchBar.tintColor = [UIColor purpleColor];
searchBar.barTintColor = [UIColor purpleColor];
[searchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
searchBar.placeholder = #"Search here";
searchBar.delegate = self;
UITextField *txfSearchField = [searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor purpleColor];
//[txfSearchField setLeftView:UITextFieldViewModeNever];
[txfSearchField setBorderStyle:UITextBorderStyleRoundedRect];
txfSearchField.layer.borderWidth = 0.9f;
txfSearchField.layer.cornerRadius = 5.0f;
txfSearchField.layer.borderColor = [UIColor cyanColor].CGColor;
//txfSearchField.background = [UIImage imageNamed:#"Navgation_bar.png"];
//[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"Navgation_bar.png"] forState:UIControlStateNormal];
//[searchBar setBackgroundImage:[UIImage imageNamed:#"Navgation_bar.png"]];
//searchBar.barStyle = UISearchBarStyleProminent;
}

UISearchBar's text field. right view not showing

What i'm trying to implement is to move UISearchBar's magnifier icon from the left edge of the text field to the right edge. This is how i do that:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FRSearchMaxHeight)];
searchBar.delegate = self;
searchBar.placeholder = NSLocalizedString(#"Search friends", "");
searchBar.showsCancelButton = NO;
UIView *view = [searchBar subviewConformingToProtocol:#protocol(UITextInput)];
UIView<UITextInput> *textInputView = (UIView<UITextInput> *)view;
[textInputView setReturnKeyType:UIReturnKeyDone];
[textInputView setKeyboardAppearance:UIKeyboardAppearanceAlert];
tableView.tableHeaderView = searchBar;
UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:0.9f] size:searchBar.frame.size];
[searchBar setBackgroundImage:image];
textInputView.layer.cornerRadius = 0.f;
textInputView.layer.borderWidth = 1.f;
textInputView.layer.borderColor = [UIColor lightGrayColor].CGColor;
UIView *magnifier = [(UITextField *)textInputView leftView];
[(UITextField *)textInputView setRightView:magnifier];
[(UITextField *)textInputView setLeftViewMode:UITextFieldViewModeNever];
[(UITextField *)textInputView setRightViewMode:UITextFieldViewModeAlways];
however the magnifier is never shown. It vanishes. I checked frame like this:
NSLog(#"FRAME %#", NSStringFromCGRect(((UITextField *)textInputView).rightView.frame));
and it says: FRAME {{0, 0}, {12.5, 12.5}}. What am i missing?

Searchbar in tableView and reload data

I can not search because my search bar is a header of tableView and then I reload tableView I have not search results.
I know there is a lot of ways to solve it but what is more wisely?
My code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
view.opaque = NO;
view.backgroundColor = [UIColor clearColor];
self.searchBar.opaque = NO;
self.searchBar.translucent = NO;
self.searchBar.backgroundColor = [UIColor clearColor];
[view addSubview: self.searchBar];
self.searchBar.barStyle = UISearchBarStyleDefault;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ;
self.searchBar.barTintColor = [UIColor clearColor];
return view;
}
And I want that then I scroll tableView my searchBar will disappear in top and then I scroll up it will appear. Is any simple to do it?
You need to use tableHeaderView and not section header. Just put in your viewDidLoad
UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.tableView.tableHeaderView = searchBar;

Auto position subviews of UINavigationBar's titleView

I have a custom titleView for displaying Navigation title and sub-title.
The title font is bigger than sub-title.
Here's what I have now:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 0, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = #"Title";
titleView.adjustsFontSizeToFitWidth = YES;
CGRect subtitleFrame = CGRectMake(0, 24, 200, 20);
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = #"subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[_headerTitleSubtitleView addSubview:titleView];
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
I want to have the ability to remove subTitleView at some point in my code,
but that makes the titleView look out of center (vertically).
So my question is, how would you implement auto-positioning here?
should I use layoutSubviews ?
if so, what would be the proper way to override it?
I thought that when the container view has an autoresizingMask it will adjust it's size according to the "total" size of it's subviews (?).
Thanks
I was able to resolve this issue by creating a separate view class and
implement layoutSubviews.
Title label subview is always attached.
Subtitle label subview can be removed/added whenever need - which
will call layoutSubViews to make re-position the Title label view in center.
- (id)initWithFrame:(CGRect)frame
{
CGRect containerFrame = CGRectMake(0, 0, 200, 44);
self = [super initWithFrame:containerFrame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.autoresizesSubviews = YES;
_title = [[UILabel alloc] initWithFrame:CGRectZero];
_title.backgroundColor = [UIColor clearColor];
_title.font = [UIFont boldSystemFontOfSize:20];
_title.textAlignment = UITextAlignmentCenter;
_title.textColor = [UIColor whiteColor];
_title.shadowColor = [UIColor darkGrayColor];
_title.shadowOffset = CGSizeMake(0, -1);
_title.adjustsFontSizeToFitWidth = YES;
_subTitle = [[UILabel alloc] initWithFrame:CGRectZero];
_subTitle.backgroundColor = [UIColor clearColor];
_subTitle.font = [UIFont boldSystemFontOfSize:13];
_subTitle.textAlignment = UITextAlignmentCenter;
_subTitle.textColor = [UIColor whiteColor];
_subTitle.shadowColor = [UIColor darkGrayColor];
_subTitle.shadowOffset = CGSizeMake(0, -1);
_subTitle.adjustsFontSizeToFitWidth = YES;
self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[self addSubview:_title];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (([self.subviews count] > 1) && ([[self.subviews objectAtIndex:1] isKindOfClass:[UILabel class]]))
{
[[self.subviews objectAtIndex:0] setFrame:CGRectMake(0,0,200,24)];
[[self.subviews objectAtIndex:1] setFrame:CGRectMake(0,24,200,20)];
}
else
[[self.subviews objectAtIndex:0] setFrame:self.bounds];
}

Resources