Programing

iPad에서 UITableView backgroundColor 항상 흰색

crosscheck 2020. 10. 18. 08:26
반응형

iPad에서 UITableView backgroundColor 항상 흰색


저는 프로젝트를 진행하고 있습니다. 나는 UITableView선명한 색상으로 설정된 s 가 많이 있습니다. 그들의 뷰의 배경색은 내 사용자 정의 색상으로 설정되어 있으며 iPhone에서는 모든 것이 정상입니다 .

iPad 에서 문제가 발생합니다 ! 나는 거의 모든 것을 시도했지만 내 UITableView색은 흰색입니다.

UITableView backgroundColor 항상 iPad 에서 회색과 같은 다른 주제를 확인 했지만 아무것도 작동하지 않았습니다. 그리고 내 문제는 회색이 아니라 눈처럼 하얗다!

그 이유는 무엇일까요?


좋은 소식 : 출시 노트에 따르면 iOS 10의 경우 :

iPad에서 실행할 때 이제 스토리 보드의 UITableViewCell에 설정된 배경색이 적용됩니다.

버전 <10 :

나는 이것을 iOS 8 (8.3)에서보고 있었다. IB에서 내 셀은 "선명한 색상"이고 콘텐츠보기는 "선명한 색상"이지만 흰색으로 렌더링됩니다. 여전히 IB에서 값을 가져 오기 때문에 불완전하지만 합리적인 솔루션 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.backgroundColor = cell.contentView.backgroundColor;
    return cell;
}

대기열에서 빼낸 재사용 가능한 셀의 배경이 iPad에서 강제로 흰색으로 표시되는 것 같습니다. 뷰 계층 디버거를 사용하여 이것을 확인할 수있었습니다.

이 작업을 수행하면 테이블의 배경색을 사용할 수 있었고 배경보기를 설정할 필요도 없었습니다.


appDelegate 파일에서 모양 API 설정을 만들어이 문제를 해결할 수 있습니다.

빠른:

UITableViewCell.appearance().backgroundColor = UIColor.clearColor()

배경색을 설정하는 대신 다음과 같이 배경보기를 사용해보십시오.

- (void)viewDidLoad {
    self.tableView.backgroundView = [UIView new];
    self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

backgroundColor를 사용하는 것이 항상 효과를 내지는 않지만 대신 배경보기를 설정하면 문제가 발생했습니다.


Ben Flynn의 답변을 바탕으로 ... cell.contentView 배경색이 반드시 cell.background 색상과 같지는 않습니다. 어떤 경우에는 더 많은 상황에서 iPad 흰색 배경 문제를 해결하는 데 이것이 효과가 있음을 알았습니다.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        cell.backgroundColor = cell.backgroundColor;
        return cell;
    }

진술이 우스꽝스럽고 미친 것처럼 보이지만 문제를 해결합니다.


내부에 많은 다른 셀이있는 테이블보기가 있으며 각 셀에는 다른 색상이 있습니다.

@Ben Flynn의 대답 cell.backgroundColor = self.contentView.backgroundColor은 그것을 달성 할 수 없습니다. 이유는 self.contentView.backgroundColornil이므로 cell.backgroundColor = nil.

기본적으로 Xcode의 버그입니다 (예, 짜증납니다!). cell.backgroundColor여전히 색상이 있지만 표시 할 수 없습니다.

@Ray W 답변을 기반으로 잠시 동안 디버그 한 후 여기에 내 솔루션이 있습니다.

@impelement YouCustomCellClass

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = self.backgroundColor; // What the heck?? But it is.
}

@end

이것은 나를 위해이 문제를 해결합니다.

 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:

enter image description here


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

참고URL : https://stackoverflow.com/questions/27551291/uitableview-backgroundcolor-always-white-on-ipad

반응형