UISearchBar background color overlaps Navigation Bar - ios

As you can see my uisearchbar is overlapping my nav bar. There is also a slim black line that appears at the bottom of the searchbar. I've tried
_itemSearchBar.searchBarStyle = UISearchBarStyleMinimal;
and it fixes the appearance, but I'm unable to set the background color inside the textfield.
if(!_itemSearchBar)
{
_itemSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
_itemSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_itemSearchBar.placeholder = #"What are you looking for?";
_itemSearchBar.delegate = self;
_itemSearchBar.barTintColor = [UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[searchBarView addSubview:_itemSearchBar];
self.navigationItem.titleView = searchBarView;
UITextField* sbTextField;
for (UIView *subView in _itemSearchBar.subviews){
for (UIView *ndLeveSubView in subView.subviews){
if ([ndLeveSubView isKindOfClass:[UITextField class]])
{
sbTextField = (UITextField *)ndLeveSubView;
//changes typing text color #cf5300
UIColor *color = [UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0];
sbTextField.textColor = color;
sbTextField.backgroundColor = [UIColor colorWithRed:130/255.0 green:58/255.0 blue:23/255.0 alpha:1.0];
//changes placeholder color
sbTextField.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:#"What are you looking for?"
attributes:#{NSForegroundColorAttributeName:color}];
break;
}
}
}

Related

TextField text Color change in searchbar

Please check my below code, I use one navigationbar which is consist a searchbar and searchbar have a textfield, now i want to change a text color of textfield and i did a below code but it is not working so please help to correct it.
[[UISearchBar appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]]setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]]setTextColor:[UIColor whiteColor]];
If you are using a native UISearchbar control then you may change text color with the code below
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]}];
Or you can do it for a specific searchBar via Identity Inspector:
Try this,
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField*)view;
UIImageView *imgView = (UIImageView*)textField.leftView;
imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imgView.tintColor = [UIColor whiteColor];
txt.backgroundColor = [UIColor blackColor];
txt.textColor = [UIColor RedColor];
UIButton *btnClear = (UIButton*)[textField valueForKey:#"clearButton"];
[btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
btnClear.tintColor = [UIColor whiteColor];
}
}

Navigation Bar's Title View Overlaps

I have added a custom titleView in Navigation Bar.
In every screen change I am adding another title View.
The problem is that the previous one is not get removed and I can See all Views at a time.
Here is my code:
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font =kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
My Problem:
Sign Up and Sign In overlaps
Solution 1: each time you have to remove the custom UIView object added to the navigation bar
[self.navigationController.navigationBar.subviews enumerateObjectsWithOptions:0 usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isMemberOfClass:[UILabel class]]) {
[obj removeFromSuperview];
}
}];
UILabel *lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
Solution 2 : add the property in a class interface to store access to custom subview in the navigationBar
#property (weak, readwrite, nonatomic) UILabel *navSubView;
[self.lblTitle removeFromSuperview];
self.lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
Or you could just use the NavigationItem's "titleView" property
UILabel *titleLabel = [[UILabel alloc] init];
UIView *titleView = [[UIView alloc] initWithFrame:titleLabel.frame];[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
This will ensure only one instance of title label exists, and it won't overlap the self.title either

Cannot change searchbar text color, but I am able to change background color

I have a searchbar embedded in a navigationbar. I'm able to change background color, but for the life of me cannot change text color. I have the following code in my viewdidload. What is missing to get the text to change?
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 *txfSearchField = [itemSearchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor colorWithRed:130/255.0 green:58/255.0 blue:23/255.0 alpha:1.0];
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:itemSearchBar
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;
I've read that the following should work but doesn't:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
I've also tried:
itemSearchBar.tintColor = [UIColor redColor];
txfSearchField.textColor = [UIColor redColor];
Any suggestions???
I think you are looking at the color of placeholder text and not typing anything in the textfield. Otherwise, your code seems correct. The method
txfSearchField.textColor = [UIColor redColor]
must work. Type anything in the field and you will see red color.

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?

Resources