UISearchBar Properties - ios

I am working on a legacy application and can't figure out why a UISearchBar is not showing up correctly.
I am trying to make the background be white or any other color but can't seem to set it correctly. How can I set the background so it's not dark as shown below?
Here is what I have tried:
[self.searchController.searchBar setSearchBayStyle:UISearchBarStyleDefault];
[self.searchController.searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CFImageRef) ([UIColor whiteColor])]];
The search bar is added in programmatically so I can't use the attributes inspector.
Alternatively, is there a way to print what the search bar properties are for style, etc via NSLog?

Every search bar view contains a textfield and here you see background color of this textfield. By default it uses tintColor.
To set manually you need to create a method for recursively finding up textfield and then background color on it.
- (UITextField*)searchSubviewsForTextFieldIn:(UIView*)view
{
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchedTextField;
for (UIView *subview in view.subviews) {
searchedTextField = [self searchSubviewsForTextFieldIn:subview];
if (searchedTextField) {
break;
}
}
return searchedTextField;
}
And then call this method as:
[[self searchSubviewsForTextFieldIn:self.searchController.searchBar] setBackgroundColor:[UIColor whiteColor]];
Hope it will work!

I have created an extension for UISearchBar customisation
Link: https://github.com/Rakshith-Nandish/SearchBarCustomiser
Also to change the UITextField background color you can use this
let textFieldSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.backgroundColor = <your desired color value>

Related

Automatically disable UIButton with type custom when a UIAlertController covers the screen

I have a UIViewController containing multiple UIButton's of type custom. In cases where my view looses focus like a new view controller gets presented or some other view is added on top of it, how do I gray out(disable) my custom buttons automatically?
Basically I want to gray out the buttons which cannot receive touch and so the user focus is only on the interactive elements on the screen.
I am not using a button type with system because I want to set non-monochrome images for button image.
When UIAlertController shows up this changes tint color of the window. That is why non custom buttons are dimmed. These normal and dimmed colors are taken from tintColor property. In case of a custom button you can override tintColorDidChange method:
class CustomButton: UIButton {
override func tintColorDidChange() {
if tintAdjustmentMode == .dimmed {
// modify subviews to look disabled
} else {
// modify subviews to look enabled
}
}
}
But in your case it looks like your image has just one color. And that is why you can make it simpler. Just load button's image with rendering mode UIImageRenderingMode.alwaysTemplate and change button's tintColor be equal to the image's color. Then everything will happen automatically.
Making image be rendered in template mode is pssible with a help of Xcode
or get it programmatically originalImage.withRenderingMode(.alwaysTemplate) from original image.
You can use following code.
NSArray *subviews = [self.view subviews];
for (UIView *v in subviews) {
if ([v isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *) v;
[button setEnabled:NO];
[button setAlpha:0.5];
}
}
This code will disable all the UIButtons in current view. You can re-enable the buttons when Alertview is dismissed.
Hope this helps!!
If you want button as untouchable You can use
button.enabled = false ;
button.userInteractionEnabled = false
before presenting your UIAlertViewController and
button.enabled = true ;
button.userInteractionEnabled = true
after presenting UIAlertViewController
and also if you want to change the color of button you can use
[btn setBackgroundImage:[UIImage imageNamed:#"your gray image"] forState:UIControlStateSelected];
or
[btn setBackgroundImage:[UIImage imageNamed:#"your gray image"] forState:UIControlStateHighlighted];
according to your choice

Appearance Proxy overridden when resetting text

I am setting up my UI by using the UIAppearance Proxy. I setup my UITextfields to have a custom font like so:
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setTextColor:[UIColor rds_purple]];
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setFont:[UIFont rds_ralewayWithSize:20.0]];
This works perfectly, and when my textfields appear on screen they have the right font. However I enable my user to shuffle the strings that are in the textfields like this:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
self.randomStringTextfield.text = [self randomString];
}
When this happens the font goes from being my custom font to being the default black font. Why is this happening, and what is the solution to get my new string to be in my custom font that I setup in the appearance proxy?
According to the Apple developer docs:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
So this solution works by removing everything and adding it back, but there is probably a better way:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
self.randomStringTextfield.text = [self randomString];
[self refreshViews];
}
- (void)refreshViews {
for (UIWindow *window in [UIApplication sharedApplication].windows) {
for (UIView *view in window.subviews) {
[view removeFromSuperview];
[window addSubview:view];
}
}
}

iOS 8 - setting the inner color of a UISearchBar

I am trying to make a search bar fill the full screen width and be one solid color in a rectangle shape. However, setting the 'tint color' only sets the color of the search bar border.
How can the inner color be set too, either in Swift or directly in xCode 6? This could be done using a text field instead, but I prefer the search bar for maintainability and its extra features.
Set an image in your search bar's textField.background and this should do the trick. e.g.
for (UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(#"UITextField")]) {
UITextField *textField = (UITextField *)subView;
textField.background = [UIImage imageNamed:#"Your Image"];
}
}

IOS 7 - Search Display Controller in TableView White Scrollview Background - Cannot change background color

In my app I'm using dark table and cells. I've set everything to black in my storyboard, and when it all works perfect until I add a Search Display Controller. The search box itself is the desired color, but when I pull down, I see a scroll view that is white. I cannot find anywhere in the storyboard settings to change that background color. I've checked several places to include searching this site in depth. Any help would be greatly appreciated.
I've gone as far as calling this helper I found on stackoverflow from viewDidLoad and programmatically setting all backgrounds to black. It still shows up as white. I've tried this for the self.view self.tableView self.searchDisplayController and still no dice.
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
[view setBackgroundColor:[UIColor blackColor]];
for (UIView *subview in subviews)
{
[subview setBackgroundColor:[UIColor blackColor]];
NSLog(#"%#, %#", [subview class], subview.backgroundColor);
[self listSubviewsOfView:subview];
}
}
Accidentally I found a solution to this problem. If you set the backgroundView property of the table view to anything, for example:
self.tableView.backgroundView = [[UIView alloc] init];
the white background disappears and backgroundColor is visible as expected.
Use this method .. it will call when you start searching..
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundColor = [UIColor anyColor];
}
You need to remember that while searching, another table view is loaded by the searchDisplayController.
So you may need to set the backgroundColor of the searchResultsTableView, like this:
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor purpleColor]]; //Purple couldn't be a good choice :)

Adding UISearchBar with Scope button as subview to UIView

I want to add UISearchbar with two scoop button to my UIView. i do not want to either navigation or table view.
when user open model view page from sheet comes up with Search option and rest. when user click on Search button scoop button to be visible.
i tried to do but i am not getting desire result.
how to remove Background of scope button, i have removed uisearchbar, similar way i do not want white background for button. is it possible. showsScopeBar i am making FALSE still background can be visible.
To remove Background image of scope buttons just try :
Class segmentedControlBackgroundView = NSClassFromString(#"_UISegmentedControlBackgroundView");
for(UIView *view in self.searchBar.subviews) {
if([view isKindOfClass:[UISegmentedControl class]]){
for(UIView *subview in view.subviews) {
if ([subview isKindOfClass:segmentedControlBackgroundView]) {
subview.hidden = YES;
}
}
}
}

Resources