UIAppearance proxy problems with appearanceWhenContainedIn - ios

I am trying to give each label in my background view a shadow:
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];
The problem is that in my background view there are some subviews (a tableview for example) which cells' labels should not get this shadowColor.
I tried this by doing so:
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setShadowColor:[UIColor clearColor]];
But the text-shadow still exists in the tableviews' cells.
Can anybody tell me what I am doing wrong?!?

You can't use the UIAppearance proxy to customise UILabel at all. See this question. Attempting to do so, in my experience, leads to inconsistent and confusing results.
(I also saw the problem where setting appearanceWhenContainedIn:[somethingElse] on UILabel causes all other [UILabel appearance] calls to be ignored)

I would create a sub class of UILabel and set the shadow appearance on that.

I think you have two options:
You could enclose that modified controls in own container and use:
#implementation ChildViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[UILabel appearanceWhenContainedIn:self.class, nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:self.class, nil] setShadowOffset:CGSizeMake(5.0, 5.0)];
}
#end
Changes will be applied only to UILabel instances hosted within ChildViewController container
Or you could subclass UILabel as suggested to avoid chaining appearance changes within your current container (so other labels in e.g. in cells are not affected).

Related

UIsearchbar show a line below it which never goes

I have a UISearchBar whose bartintcolor i have changed and have cancel enabled. This is added as a subview of a UIVew.
whenever make search bar visible, it shows a black line below it which never goes away.
Am i missing something about UISearchbar appearence or its background view.
dummyview=[[UIView alloc]initWithFrame:CGRectMake(10, 30,self.headerView.frame.size.width-20, 30)];
[dummyview setBackgroundColor:[UIColor redColor]];
[dummyview setClipsToBounds:TRUE];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, dummyview.frame.size.width, dummyview.frame.size.height)];
self.searchBar.delegate = (id)self;
[self.searchBar setPlaceholder:#"Search"];
self.searchBar.showsCancelButton = YES;
_searchBar.barTintColor = [Utilities colorWithHexString:#"3a7ebc"];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
_searchBar.delegate=self;
[dummyview addSubview:_searchBar];
it gives me like this
if i change background to native one, i get displaced content.
// _searchBar.barTintColor = [Utilities colorWithHexString:#"3a7ebc"];
I just dont want the black line below the searchbar.
Updated:
using
_searchBar.searchBarStyle=UISearchBarStyleMinimal;
_searchBar.barStyle=UIBarStyleDefault;
result in this
So
finally everything done
_searchBar.searchBarStyle=UISearchBarStyleMinimal;
_searchBar.barStyle=UIBarStyleDefault;
UITextField *searchField=[_searchBar valueForKey:#"_searchField"];
if (searchField) {
[searchField setTextColor:[UIColor whiteColor]];
}
Put my code in viewdidload or viewwillappear method.
searchbar.barTintColor=[UIColor samebgcolor];
searchbar.layer.borderWidth = 1;
searchbar.layer.borderColor = [[UIColor samebgcolor] CGColor];
I would also consider, "I hope this will help you", as this is the strongest, and hopes that the thing will most definitely, certainly help.
If you like my Answer so accept and upvote my answer

UISearchBar text color when used in UISearchDisplayController

i'm trying to change a UISearchbar text color in iOS7 app. (i want the text to be white).
the search bar is attached to a searchDisplay controller.
i have tried many options, including this one:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
also tried setTintColor:, and setBarTintColor:. Nothing works.
Note, this solutions does work when the search bar is not attached to a displayController.
FYI, not that i think it matters, but i use a background image for my search bar.
in addition, the searchbar is placed in my navigationBar.
here is my code:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 52)];
[_searchBar setSearchFieldBackgroundImage:[IMAGE(#"searchBarBg") resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)] forState:UIControlStateNormal];
_displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_displayController.delegate = self;
_displayController.displaysSearchBarInNavigationBar = YES;
I'd appreciate anyones help. thanks!

Globally set background color of buttons, except accessory buttons in UITableView

I am using the below code to make all my buttons in an application the same color. However the accessory icon in the UITTableView row also has it. Is there a way to ignore it in the table view?
[[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];
A UIView conforms to the UIAppearanceContainer protocol.
So you should use appearanceWhenContainedIn: to distinct buttons depending on where they are contained in.
[[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];
[[UIButton appearanceWhenContainedIn:[UITableView class], nil] setBackgroundColor:nil];
It would be best to use the UITableViewCell subclass instead of the table view.
You can use the
[[UIButton appearanceWhenContainedIn: [UITableView class]] setBackgroundColor:[UIColor differentColor]];

How to change text color of table header on iOS 5?

How to change text color of table header on iOS 5 without using viewForHeaderInSection to create entire header view?
Add on viewDidLoad:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setShadowColor:[UIColor clearColor]];

Change text color of search bar ios

Is it possible to change the text color of the search bar? I don't seem to have access to the UISearchBarTextField class...
firstly, you find the subview in UISearchBar, then find the UITextField in sub View then change color
Try this code:-
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UITextField.class]){
[(UITextField*)subView setTextColor:[UIColor blueColor]];
}
}
for Ios 5 +
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
As of iOS 5, the right way to do this is using the appearance protocol.
For example:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
You can set the attributes like so, call this in your controller.
[[UITextField appearanceWhenContainedIn:[self class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
*note that this will change all UITextFields in your controller
The original UISearchBar hierarchy has changed since the original post and the UITextField is no longer a direct subview. The below code makes no assumptions about the UISearchBar hierarchy.
This is also useful when you don't want to change the search bar's text color throughout the entire application (i.e. using appearanceWhenContainedIn).
/**
* A recursive method which sets all UITextField text color within a view.
* Makes no assumptions about the original view's hierarchy.
*/
+(void) setAllTextFieldsWithin:(UIView*)view toColor:(UIColor*)color
{
for(UIView *subView in view.subviews)
{
if([subView isKindOfClass:UITextField.class])
{
[(UITextField*)subView setTextColor:color];
}
else
{
[self setAllTextFieldsWithin:subView toColor:color];
}
}
}
Usage:
[MyClass setAllTextFieldsWithin:self.mySearchBar toColor:[UIColor blueColor]];

Resources