Appearance Proxy overridden when resetting text - ios

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

Related

iOS Scope Bar does not appear after added under UISearchBar

I added scope bar under my search bar. However, scope bar does not appear. Any idea? Please help.
- (UISearchBar *)searchBar{
if (!_searchBar) {
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
[_searchBar setBackgroundImage:[UIImage imageNamed:#"ic_searchBar_bgImage"]];
[_searchBar sizeToFit];
[_searchBar setPlaceholder:#"Search"];
[_searchBar.layer setBorderWidth:0.5];
_searchBar.showsScopeBar = YES;
_searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"All", #"Near Me", #"Coming Soon", nil];
[_searchBar.layer setBorderColor:[UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1].CGColor];
[_searchBar setDelegate:self];
[_searchBar setKeyboardType:UIKeyboardTypeDefault];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Bitter" size:14],
}];
}
return _searchBar;
}
EDITED:
I added it in Tableview header. How can I change in order to make it appear?
- (UITableView *)tableView{
if (!_tableView) {
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, kScreenHeight-49.0) style:UITableViewStylePlain];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
_tableView.tableHeaderView=self.searchBar;
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
[_tableView setTableFooterView:v];
}
return _tableView;
}
I tried to run your code and scope bar shows up fine for me.
So, while I cannot fathom from code why it should not appear, I would enlist some debugging tips here to help you see what else could go wrong that can cause this problem.
While your app is in debug running mode (iOS simulator), try this:
XCode -> Debug Menu -> View Debugging
Press Capture View Hierarchy.
It should come up with a tree structure on the left hand side, showing your table view. On the right hand side it will show you screenshot of your views
On the left hand side tree, look for the UISearchbar.
Under its hierarchy, search for the object of type: _UISearchBarScopeBarBackground. It is followed by UISegmentedControl which has segments corresponding to each scope text.
Refer to below image (this should appear in your scope hierarchy):
If UISegmentedControl appears fine here but not visible on the right side, check to see if its hidden by some other view / table view itself. Some frame adjustment should fix this issue.
If UISegmentedControl does not appear on the left side itself, you seem to have serious problem. Probably your outlets are not initialised and you may want to check if your table view is properly initialised and all. In any case this is very less likely to occur and it's only possible if nothing at all was visible on your simulator screen.

UISearchBar Properties

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>

Can't remove UILabel from UINavigationController.view

I'm adding a tiny marker under my UINavigationController title so the user will know that the title is tappable. You can see in the code below how I add this label to the navigation bar.
_labelCalendarMenuArrow = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 5, 30, 10, 26)];
_labelCalendarMenuArrow.text = #" ̬";
_labelCalendarMenuArrow.font = [UIFont fontWithName:#"HelveticaNeue" size:30];
_labelCalendarMenuArrow.textAlignment = NSTextAlignmentCenter;
_labelCalendarMenuArrow.textColor = [UIColor whiteColor];
[self.navigationController.view addSubview:_labelCalendarMenuArrow];
The problem is that I'm unable to remove this UILabel from the navigationController.view when leaving this screen. In the code below you can see how I try a few methods for hiding or removing this UILabel, but none of them work... The UILabel will stay in the NavigationController until I go to a different stack of views and come back. Any advice?
- (void)viewWillDisappear:(BOOL)animated {
[_labelCalendarMenuArrow removeFromSuperview];
_labelCalendarMenuArrow = nil;
_labelCalendarMenuArrow.alpha = 0;
}
A simple solution can be to use HIDDEN property
- (void)viewWillDisappear:(BOOL)animated
{
_labelCalendarMenuArrow.hidden=YES;
}
What you are trying to do here is fairly horrific, adding a view within a parent navigation controller's view is against all sense.
Please read apple's human interface guidlines as there is a better solution to signifying that the title is clickable in there. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/
If you still insist on adding a label underneath the navigation bar's title label you should implement a titleView for the navigation item. Within that view you will have to include your own title label to replace the original and then your signifier label underneath.

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 :)

iOS 7 UISearchBar right spacing

Don't know why, my search bar in iOS 7 is leaving a right space. It's ok in iOS 6.
I know it has something to do with the section index, because if I remove it the space disappears, but I don't know how to fix it. Any thoughts?
Embed your UISearchBar in a UIView and then add that as the tableHeaderView. Structuring it that way in a storyboard worked for me. I'm guessing iOS resizes the UISearchBar in the tableHeaderView, but leaves a basic UIView alone (and doesn't bother to look inside it).
You might also want to make the section index transparent, which I did with:
[[UITableView appearance] setSectionIndexBackgroundColor:[UIColor clearColor]];
[[UITableView appearance] setSectionIndexTrackingBackgroundColor:[UIColor clearColor]];
Until a better answer appears, I just manually changed the frame of the search bar like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect barFrame = self.searchBar.frame;
barFrame.size.width = self.view.bounds.size.width;
self.searchBar.frame = barFrame;
}
I had this same issue with the iPhone 6/ 6Plus when using a SearchDisplayController. (Using Swift)
I tried setting the frame of the search bar but with no luck but i noticed that if i tapped on the textField of the UISearchBar and then cancelled it then it would take on the proper size of the view. I therefore managed to fix the issue by calling the code below in ViewDidLoad of the viewController using the search.
self.searchController.setActive(true, animated: false)
self.searchController.setActive(false, animated: false)
self.contactsTableView.sectionIndexBackgroundColor = [UIColor clearColor];
The reason for that white edge is because your index layer has a white background and is on top of the search bar. This should be sufficient.
Add the search bar inside a UIView put as tableView's header view. Set the tableview's sectionIndexBackgroundColor to clear color because it covers the header.
Tested with iOS 7, 7.1;
Because the table view always leaves 15px on the right for section Indexes View, so you should resize the Seach bar after reloading the table view
First:
self.tblData.sectionIndexBackgroundColor = [UIColor clearColor]; //(iOS >= 7 only)
Cheating time:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self performSelector:#selector(resizeSearchBar) withObject:nil afterDelay:0.01];
}
- (void) resizeSearchBar
{
CGRect frame = self.searchBar.frame;
if (frame.size.width < self.tblData.frame.size.width) {
frame.size.width = self.tblData.frame.size.width;
}
self.searchBar.frame = frame;
}
- (void) reloadTableData // call it anytime you want to reload table view
{
[self.tblData reloadData];
[self performSelector:#selector(resizeSearchBar) withObject:nil afterDelay:0.01];
}
Suggest
Dont cheat like me, just do the simpler way:
self.searchBar.searchBarStyle = UISearchBarStyleMinimal; // iOS >= 7 only
I also attached a UISearcBar in my application, and nothing is wrong there even my application supports rotation also.
Could you try removing and re creating UISearchBar in storyboard/xib
I added the search bar as a subview of the top-level view instead of the table view. Used autolayout to pin the searchbar to the top guide, and a vertical space constraint of 0 between the search bar and the table view.
The accepted solution with the method viewDidLayoutSubviews makes the screen flicker.
Instead what I did was create a subclass of UISearchBar that simply does this:
FullWidthSearchBar.h:
#interface FullWidthSearchBar : UISearchBar
#end
FullWidthSearchBar.m:
#import "FullWidthSearchBar.h"
#implementation FullWidthSearchBar
- (void)setFrame:(CGRect)frame {
frame.size.width = self.superview.bounds.size.width;
[super setFrame:frame];
}
#end
And then I assigned that class to the search bar on my xib:
The problem is the right white block, so if we change the block color the same as the search bar background, it looks normal.
just
if (IOS7) {
self.tableview.backgroundColor = [UIColor colorWithPatternImage:self.searchBar.backgroundImage];
}

Resources