UISearchBar's text field. right view not showing - ios

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?

Related

UISearchBar background color overlaps Navigation Bar

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;
}
}
}

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;
}

ios7 searchBar in navigationBar cover rightbuttonItem

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.

How to fix offset keyboard when UISearchbar is touched

The problem I'm having is that when a user touches into the search bar the keyboard appears fine. Then when the presentQR method (see below) is called and then dismissed and then the searchbar is touched the keyboard appears as shown in the screenshot where it is offset. This is iOS7; not sure if that matters though.
I'm adding a UISearchbar using the following code:
// search bar
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 200.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 190, 40)];
searchBar.backgroundImage = [[UIImage alloc] init];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = searchBar;
searchBar.delegate = self;
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIColor whiteColor],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal];
UITextField *textfield = [[UITextField alloc] init];
searchBar.placeholder = #"City Search";
// hide magnifying glass
textfield = nil;
for (UIView* subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
textfield = (UITextField*)subView;
[(UITextField*)subView setTextColor:[UIColor whiteColor]];
[(UITextField*)subView setFont:[UIFont fontWithName:#"DIN-Regular" size:18]];
textfield.leftViewMode = UITextFieldViewModeNever;
textfield.rightViewMode = UITextFieldViewModeAlways;
break;
}
}
UIButton *cancelButton;
for (id button in searchBar.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
cancelButton=(UIButton*)button;
break;
}
}
[searchBar addSubview:textfield];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
-(void)presentQR {
QRReaderViewController *qr = [[QRReaderViewController alloc]initWithNibName:#"QRReaderViewController" bundle:nil];
UIButton *removeQRBtn=[UIButton buttonWithType:UIButtonTypeCustom];
removeQRBtn.frame=CGRectMake(260, 10, 60, 40);
[removeQRBtn addTarget:self action:#selector(removeQR) forControlEvents:UIControlEventTouchDown];
[removeQRBtn setImage:[UIImage imageNamed:#"qrcode.png"] forState:0];
[qr.view addSubview:removeQRBtn];
qr.view.backgroundColor = customColorGrey;
qr.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
// [self presentModalViewController:qr animated:YES];
[self presentViewController:qr animated:YES completion:nil];
}
-(void)removeQR {
// [self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
1st Make sure that in QRReaderViewController there is nothing like reframing window or such. There might be an element conflicting with UIWindow preventing keyboard to be shown correctly. (e.g unbalanced orientation changes.)
2nd If you didn't find anything there you could try accessing the keyboard view using the following code and attempt to reset the frame. You could put this code in keyboardWillShow method (need to register for the notification)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/c/data/UIKeyboardWillShowNotification
PS. I would suggest to just log the keyboard frame so you know when it's changed.
Reframing Keyboard: Found here: https://stackoverflow.com/a/10957843/1017340
//The UIWindow that contains the keyboard view - It some situations it will be better to actually
//You need to iterate through each window to figure out where the keyboard is, but In my applications case
//I know that the second window has the keyboard so I just reference it directly
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
//Because we cant get access to the UIPeripheral throughout the SDK we will just use UIView.
//UIPeripheral is a subclass of UIView anyways
UIView* keyboard;
//Iterate though each view inside of the selected Window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
//Get a reference of the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
//Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard"
if([[keyboard description] hasPrefix:#"<UIPeripheral"] == YES)
{
NSLog(#"Keyboard Frame: %#",NSStringFromCGRect(keyboard.frame));
//HERE : try reframing the keyboard
//[keyboard setFrame:CGRectMake(...)]; /*NOT TESTED NOT RECOMMENDED*/
}
}

ScrollView, Views not showing up

The scroll view shows up, without any of the views visible. The view moves fine, the page control works. When i change the scroller background that all works fine. The views "the different colours" don't show. Have no idea whats wrong. Thanks for the help
-(void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 215, WIDTH_OF_IPHONE, HEIGHT_OF_SCROLLER);
int numOfPhotos = 6;
scrollViewer = [[UIScrollView alloc] initWithFrame:frame];
scrollViewer.contentSize = CGSizeMake((WIDTH_OF_IPHONE*numOfPhotos), HEIGHT_OF_SCROLLER);
scrollViewer.pagingEnabled = YES;
//scrollViewer.backgroundColor = [UIColor lightGrayColor];
[scrollViewer setDelegate:self];
int i = 0;
while (i < numOfPhotos){
CGRect frameOfView = CGRectMake((i*320), 215, 320, HEIGHT_OF_SCROLLER);
UIView *photoFrame = [[UIView alloc] initWithFrame:frameOfView];
if (i == 0){
photoFrame.backgroundColor = [UIColor redColor];//colorWithPatternImage:image1];
NSLog(#"colour was selected");
}else if (i==1){
photoFrame.backgroundColor = [UIColor blueColor];//colorWithPatternImage:image2];
}else if (i==2){
photoFrame.backgroundColor = [UIColor purpleColor];//colorWithPatternImage:image3];
}else if (i==3){
photoFrame.backgroundColor = [UIColor orangeColor];//colorWithPatternImage:image4];
}else if (i==4){
photoFrame.backgroundColor = [UIColor magentaColor];//colorWithPatternImage:image5];
}else{
photoFrame.backgroundColor = [UIColor greenColor];//colorWithPatternImage:image6];
}
[scrollViewer addSubview:photoFrame];
//[photoFrame release];
photoFrame = NULL;
i++;
}
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH_OF_IPHONE, 15)];
pageControl.center = CGPointMake(scrollViewer.center.x, (208+HEIGHT_OF_SCROLLER));
[pageControl setNumberOfPages:numOfPhotos];
[pageControl addTarget:self action:#selector(pageSwiped:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:scrollViewer];
[self.view addSubview:pageControl];
My .H file looks like this
#interface HomePage : UIViewController <UIActionSheetDelegate, UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollViewer;
UIPageControl *pageControl;
BOOL pageControlUsed;
}
#end
Try setting the y origin of the photoFrame to 0 as it will place it under the actual frame of your scrollview.
If not the problem you are having is that you are making the photoFrame NULL. If you are using ARC you shouldn't worry as the Garbage Collector will take care of it.

Resources