UITableViewHeaderFooterView remove transparency - ios

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.

Related

Container View White Background, iPad Swift Issue

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.

UITableView backgroundColor always white on iPad

I'm working on a project. I have plenty of UITableViews which are set as clear color. Their views' background color are set to my custom color and everything is fine on iPhone.
The issue comes up on iPad! I tried almost everything, but my UITableView has a white color.
I checked the other topics, like: UITableView backgroundColor always gray on iPad, but nothing worked. Also, my problem is not grey, it's white as snow!
What might be the reason of it?
Good News: According to the release notes, for iOS 10:
When running on iPad, the background color set for a UITableViewCell
in a Storyboard is now respected.
For versions <10:
I was seeing this in iOS 8 (8.3). Even though in IB my cells were "clear color" and their content views were "clear color" they would render as white. An imperfect but reasonable solution, since it still takes values from IB:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = cell.contentView.backgroundColor;
return cell;
}
It seems that my dequeued reuseable cells get their background forced to white on iPad. I was able to determine this using the view hierarchy debugger.
Once I did this I was able to use the table's background color and didn't have to set a background view, although that works as well.
You can fix this by making an appearance API setting in your appDelegate file :
Swift:
UITableViewCell.appearance().backgroundColor = UIColor.clearColor()
Instead of setting the background color, trying using a background view instead, like this:
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}
I've had problems where using the backgroundColor doesn't always produce an effect, but setting a background view instead works fine.
Building off of Ben Flynn's answer... cell.contentView background color is not necessarily equal to the cell.background color. In which case, I found that this worked in resolving the iPad white background issue in more situations:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = cell.backgroundColor;
return cell;
}
While the statement looks ridiculous and crazy... it resolves the issue.
Swift 5
I have a table view with many different cells inside, each one has different color.
The answer from #Ben Flynn cell.backgroundColor = self.contentView.backgroundColor can not achieve that.
The reason is self.contentView.backgroundColor is nil, so what you did is just clear the cell.backgroundColor = nil.
Basically it is the bug from Xcode (I think, yeah it sucks!), cell.backgroundColor still has color, but it can not display.
After debug for a while, based on #Ray W answer, here is my solution.
class YourCustomCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = backgroundColor // Tricky to re-apply the background color
}
}
This solve this issue for me
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.backgroundColor = UIColor.clear
}
In my experience, some versions of iOS set UITableViewCell's backgroundColor before calling the delegate's tableView:willDisplayCell:forRowAtIndexPath:. Resetting back to your custom color in that method fixes it.
Swift 3.1
I've worked around this bug by placing this in my UITableViewCell subclass:
When the cell is being loaded from the NIB file:
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.clear
}
and when the cell is being reused by the system
override func prepareForReuse() {
super.prepareForReuse()
self.backgroundColor = UIColor.clear
}
iOS 10 is supposed to fix this issue in Interface Builder, as animeshporwal said.
SWIFT 3.XX
Put this
UITableViewCell.appearance().backgroundColor = UIColor.clear
In AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
I'm using Storyboard with UITableViewControlrs, so the most simple decision was to subclass all controllers, and add this method to parent
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
cell.backgroundColor = [UIColor clearColor];
}
}
SWIFT
This was a happening to me, too. My table view in my SWRevealViewController appeared white on my iPad when it looked clear (which is how I wanted it with a background image) on my iPhone. I tried all of the above but this is what ended up working for me in my viewDidLoad().
tableView.backgroundView = UIImageView(image: UIImage(named: "gray"))
tableView.backgroundView?.backgroundColor = .clearColor()
UITableViewCell.appearance().backgroundColor = .clearColor()
This can be achieved in a Storyboard as follows:
Show the document outline for your storyboard
Within your table, pick a TableViewCell
go to the Content View within that Cell
Set the background colour of the Content view.
Result: iPad and iPhone simulator views look the same
I just had this issue and fixed it by changing the backgroundColor of the View (as opposed to the one of the tableView). Seems on iPad, that's the one that is used first and in my case it was set to white.
SWIFT
I had this problem too. Works fine on .phone but tableViewCells go white on .pad. Thought I'd show how I fixed it using swift.
Connect The tableViewCell to the viewController.swift as an #IBOutlet like so:
#IBOutlet weak var tvc1: UITableViewCell!
#IBOutlet weak var tvc2: UITableViewCell!
#IBOutlet weak var tvc3: UITableViewCell!
Then in viewDidLoad put the following:
tvc1.backgroundColor = tvc1.backgroundColor
tvc2.backgroundColor = tvc2.backgroundColor
tvc3.backgroundColor = tvc3.backgroundColor
Very strange, I don't know whats happening here but this solved it for me.
This seems to be fixed with iOS 10 Beta 4 as mentioned in release notes under UIKit notes:
I have a transparent table view with semi-transparent table view cells. I set the table view background color to clear when I create the table. This works for all iOS/device combinations except iPad + iOS 8, where the background color remains white.
For me, setting the cell's background color to semi-transparent and the content view background color to clear works, since I do it on each tableView(_ tableView: UITableView, cellForRowAt indexPath). The problem for me was only that the table view background remained white.
I tried all combinations that I did find regarding this issue, but the only thing I actually needed to to was to set the table views background to transparent on each tableView(_ tableView: UITableView, cellForRowAt indexPath). Not super-intuitive, but at least it works. :P

how can i make headerView scroll (not stay on the top of the tableview ) accompanying with UItableViewCell when i was scrolling tableview

in plainsytle tableview , we set headerViews for each section by delegate method
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
so,when i scrolling up tableview , the first section's headerView is always showing on the top of tableview before the section totally disappear on the tableview , then the second section's headerView will show on the top of tableview instead . so my question is how can i make headerView scroll accompanying with uitableViewCell , just like group style tableview ?
You can disable scrolling sectionHeader by changing the table property to -
UITableViewStyleGrouped
You have to set it on the initialisation of UITableView.
Change Table Style from Plain to Grouped on StoryBoard
OR
UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
Just change style of table from "Plain" to "Grouped".
subclass the UITableView and override this method
- (BOOL)allowsHeaderViewsToFloat{
return NO;
}
same for footer
- (BOOL)allowsFooterViewToFloat{
return NO;
}
But I think that this is a private API ... You will not be able to submit it to the AppStore
If you will upload it to the AppStore; Then you have two other options
Adding a normal cell instead of the section header
If you have only one section, then you can simply use table header instead of section header
I had the same issue and when i browsed i came to this link See The accepted answer from and I came to conclusion that you need to set tableViewHeader Style to Group and it works Charm.
You can change the style of the tableView.
let tableView = UITableView(frame: .zero, style: .grouped)
if you ve more than one section, you can customize it using some thirdt party API
else you can try this using Grouped table instead of plain tableView.
Try like this when you assign custom view as tableView Header view it becomes header view for table and it will scroll with the table view
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
tableView.tableHeaderView =sectionView;
return sectionView;
}
Change the UITableViewStyle of the table from UITableViewStylePlain to UITableViewStyleGrouped, this will make the header scroll up with cells.
Swift:
As Hani Ibrahim pointed out you need to subclass UITableView. Apple says you :
must specify style at creation.
So:
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: .grouped)
Notice how my super.init uses .grouped as the style.
For this style of tableview (.grouped) Apple also says:
Summary
A table view whose sections present distinct groups of rows. The section headers and footers do not float.
Then you can call the following tableView delegate methods:
viewForHeaderInSection and heightForHeaderInSection
(notice those are for headers only, use the footer counterparts if you want footers too)
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel = UILabel()
headerLabel.text = "My cool header title"
headerLabel.textAlignment = .center
headerLabel.font = UIFont(name: "OpenSans-Bold", size: 16)
return headerLabel
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
And that should do it! Good luck.
Disable scrolling section header by changing the property from Storyboard
Storyboard
Attributes inspector
Style
Change Style from plain to Grouped from the dropdown

How do I apply UIAppearance Proxy properties to UILabel?

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`

UITableView, Separator color where to set?

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!

Resources