I have been getting unreliable results while trying to apply UIAppearance proxy styles to the UILabel class proxy. For example, the following works as I would expect:
[[UILabel appearance] setFont:[UIFont fontWithName:SOME_FONT size:SOME_SIZE]];
[[UILabel appearance] setShadowColor:[UIColor blackColor]];
Setting the textColor doesn't work, however, this:
[[UILabel appearance] setColor:[UIColor greenColor]];
does work. Kind of. It's somewhat unreliable and causes any instance-specific calls to setTextColor: to be ignored.
What is the correct way to apply UIAppearance styles to a UILabel?
OK, it turns out that you cannot style any UILabel properties using the UIAppearance proxy.
While the UILabel class conforms to the UIAppearanceContainer protocol, a check of UILabel.h shows that none of its properties are marked with UI_APPEARANCE_SELECTOR, the prerequisite for the use of UIAppearance.
Bugger.
I have subclassed UILabel
#interface SmallLabel : UILabel
#end
#implementation SmallLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
#end
Then I use appearanceWhenContainedIn:
UIFont *smallFont = [UIFont fontWithName:#"Arial" size:15];
[[SmallLabel appearanceWhenContainedIn:[UIView class], nil] setFont:smallFont];
This works to use the desired font for all SmallLabels in my app. I just need to set the Custom Class to SmallLabel in the XCode Identity Inspector. It does not seem to work with labels create programmatically, only those in NIBs.
After further testing this method does not always work reliably.
In Swift you do the following to customize the appearance attributes for a UILabel when contained in a UITableViewHeaderFooterView:
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 18)
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white
This will apply to the textLabel attribute when you use UITableViewHeaderFooterView in:
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerViewId)
if headerView == nil {
headerView = UITableViewHeaderFooterView(reuseIdentifier: headerViewId)
}
headerView?.textLabel?.text = "My Header".uppercased()
return headerView
}
This really helps when using the UITableViewStyle.grouped, as those section header views seem to be overridden with a default style even if you customize the UITableViewHeaderFooterView.textLabel in viewForHeaderInSection
Following code worked for me using swift 2.2 and for iOS 9.0
let textFieldInsideSearchBar = self.searchBar?.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = BCGConstants.Colors.darkBluishPurpleColor()
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor(red: 220/255, green: 209/255, blue: 231/255, alpha: 1.0)`enter code here`
Related
My TableView's header is not displaying well in iOS13. No matter what color I put, it always displays a light gray now...
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{ //Section color & style
UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;
v.backgroundView.alpha = 1;
v.textLabel.textColor = sectionColor;
v.textLabel.font = sectionFont;
v.textLabel.numberOfLines = 1;
v.textLabel.minimumScaleFactor = 0.5;
v.textLabel.adjustsFontSizeToFitWidth = YES;
v.backgroundView.backgroundColor = [UIColor blueColor];
}
iOS12:
iOS13:
It is strange because when I put a stop in the debugger in step by step, it displays me the good image in iOS13, but not in the app:
Any suggestions, thanks in advance ?
I was noticing the same thing in one of my apps.
Then saw a log message in the console:
Setting the background color on UITableViewHeaderFooterView has been
deprecated. Please set a custom UIView with your desired background
color to the backgroundView property instead.
Setting a custom UIView with the desired background color as the backgroundView of the UITableViewHeaderFooterView solved the problem.
Code sample
class SomeHeaderView: UITableViewHeaderFooterView {
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configure() {
let backgroundView = UIView(frame: .zero)
backgroundView.backgroundColor = .blue
self.backgroundView = backgroundView
}
}
This works for me.
v.contentView.backgroundColor = .blue
instead of
v.backgroundView.backgroundColor = .blue
Try to add overlay view and change this color for this view.
UIView *coloredView = [[UIView alloc] init];
coloredView.backgroundColor = [UIColor blueColor];
[v addSubview:coloredView];
[[coloredView.leadingAnchor constraintEqualToAnchor:v.leadingAnchor constant:0] setActive:YES];
[[coloredView.trailingAnchor constraintEqualToAnchor:v.trailingAnchor constant:0] setActive:YES];
[[coloredView.topAnchor constraintEqualToAnchor:v.topAnchor constant:0] setActive:YES];
[[coloredView.bottomAnchor constraintEqualToAnchor:v.bottomAnchor constant:0] setActive:YES];
My problem only occurs on iPad. By default, my container view is clear/transparent. It works and appears fine on iPhone but when displaying on iPad it defaults to a white background. The issue is the same on most custom uitableviews as well.
I've attached an image of the problem below:
This was basically a solution to my problem, only thing I had to add and I will attach the swift code was "willDisplayCell" method using tableViewDelegate.
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
var backgroundView : UIView = UIView(frame: CGRect.zeroRect)
backgroundView.backgroundColor = UIColor.clearColor()
cell.backgroundView = backgroundView
cell.backgroundColor = UIColor.clearColor()
}
It is problem with cell's background color on iPad. I'd faced with this problem.
I've fixed this problem by changing background color of all components of cells to the clear color in code.
//in cell's awakeFromNib
UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundView.userInteractionEnabled = NO;
backgroundView.backgroundColor = [UIColor clearColor];
self.backgroundView = backgroundView;
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
I think this is very helpful
The Default colour for cell's ContentView is different for iPhone and iPad.
You have to set Background colour of Cell's ContentView in your Storyboard and you are done.
I've done some searching, and a lot of answers are saying that I need to create a custom UIView and stick it in to the UITableViewHeaderFooterView.
At the moment, I'm setting the appearance in the AppDelegate like so
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
But I want to get rid of the transparency. Is there a way to do this from the appDelegate without having to go through my entire app and change the UITableViewHeaderFooterView wherever a tableView is being used?
Unfortunately, UITableViewHeaderFooterView sets the backgroundColor of its backgroundView to a slightly transparent version of its tintColor.
As a workaround, you can set the backgroundColor of any subview of the UITableViewHeaderFooterView.
[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setBackgroundColor:[UIColor redColor]];
You can now accomplish this in Swift by implementing the tableView( willDisplayHeaderView:forSection) delegate method and overriding the backgroundColor of the backgroundConfiguration like so:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerFooter = view as? UITableViewHeaderFooterView else {
return
}
headerFooter.backgroundConfiguration?.backgroundColor = <color>
}
headerFooter's UILabel is accessible via headerFooter.textLabel? if you would also like to customize the header/footer's font, color, etc.
I'm trying to customize UISearchBar but I"m having a hard time with it.
This is what my searchbar looks like currently. What I want to be able to do is set a uiimage on the entire uisearchbar. I tried setSearchFieldBackgroundImage but it does not affect the text field part of the searchbar. How can I put an image in the white part? I also want to extend the white part to the edge to have equal margins on all sides.
for the image purpose u could use this one,dont forget to import Quartzcore framework
[searchbar setBackgroundImage:[UIImage imageNamed:#"base_200x50.png"]];
[searchbar setTranslucent:YES];
UITextField *txtSearchField = [searchbar valueForKey:#"_searchField"];
[txtSearchField setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"base_200x50.png"]]];
[txtSearchField setBorderStyle:UITextBorderStyleRoundedRect];
txtSearchField.layer.borderWidth = 8.0f;
txtSearchField.layer.cornerRadius = 10.0f;
txtSearchField.layer.borderColor = [UIColor clearColor].CGColor;
and to extend textfeild u can set frame of txtSearchField.
I hope this may help you:
Pattern image I used:
#IBOutlet weak var sbSearchBar: UISearchBar!
// if you wan to set background color for searchbar, except field
func setupSearchbarBackgroundImage() -> Void {
sbSearchBar.setBackgroundImage(UIImage(named: "pattern2"), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
}
Result:
func setupSearchbar() -> Void {
// if you wan to set background color for field only
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.backgroundColor = UIColor(patternImage: UIImage(named: "pattern2")!)
}
}
Here is result:
I have added a UITableView in IB and set the "delegate" and "datasource" and all is working well. What I wanted to do next was change the separator color, but the only way I could find to do this was to add the method to one of the delegate callbacks, is there a better place I should put this?
I don't have this at the moment but I was thinking that maybe I need to add an "iVar" from my controller that I can link to the UITableView in IB and then set separator color in the viewDidload?
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setSeparatorColor:[UIColor blackColor]];
return 65;
}
- (void)viewDidLoad
{
[self.tableView setSeparatorColor:[UIColor myColor]];
}
You'll need the self. to access it, remember.
Swift 4.2
tableView.separatorColor = UIColor.red
Now you should be able to do it directly in the IB.
Not sure though, if this was available when the question was posted originally.
Swift version:
override func viewDidLoad() {
super.viewDidLoad()
// Assign your color to this property, for example here we assign the red color.
tableView.separatorColor = UIColor.redColor()
}
Try + (instancetype)appearance of UITableView:
Objective-C:
[[UITableView appearance] setSeparatorColor:[UIColor blackColor]]; // set your desired colour in place of "[UIColor blackColor]"
Swift 3.0:
UITableView.appearance().separatorColor = UIColor.black // set your desired colour in place of "UIColor.black"
Note: Change will reflect to all tables used in application.
Swift 3, xcode version 8.3.2, storyboard->choose your table View->inspector->Separator.
If you just want to set the same color to every separator and it is opaque you can use:
self.tableView.separatorColor = UIColor.redColor()
If you want to use different colors for the separators or clear the separator color or use a color with alpha.
BE CAREFUL: You have to know that there is a backgroundView in the separator that has a default color.
To change it you can use this functions:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if(view.isKindOfClass(UITableViewHeaderFooterView)){
var headerView = view as! UITableViewHeaderFooterView;
headerView.backgroundView?.backgroundColor = myColor
//Other colors you can change here
// headerView.backgroundColor = myColor
// headerView.contentView.backgroundColor = myColor
}
}
func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
if(view.isKindOfClass(UITableViewHeaderFooterView)){
var footerView = view as! UITableViewHeaderFooterView;
footerView.backgroundView?.backgroundColor = myColor
//Other colors you can change here
//footerView.backgroundColor = myColor
//footerView.contentView.backgroundColor = myColor
}
}
Hope it helps!