prosource

투명한 배경으로 UITableViewCell을 만드는 방법

probook 2023. 8. 21. 21:24
반응형

투명한 배경으로 UITableViewCell을 만드는 방법

배경색을 설정하려고 합니다.UITableViewCell투명하게하지만 아무 것도 효과가 없는 것 같습니다.내부에 몇 가지 이미지/버튼이 있습니다.tableViewCell그리고 저는 흰색을 만들고 싶습니다.grouptableview배경이 사라지면 이미지와 버튼에 대한 '슬립' 효과를 얻을 수 있습니다(마치 그것들이 내부에 없는 것처럼).tableview).

이 일이 어떻게 성사될 수 있는지 아십니까?

다른 두 가지 옵션이 모두 작동하지 않는 경우 다음을 시도합니다.

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

이것은 실제로 UITableViewCell의 문서에 답변되어 있습니다.따라서 컨트롤에 투명도를 설정해야 하지만 실제 셀 배경색은 아래에 따라 설정해야 합니다.

"참고: (UIView에서 선언한 backgroundColor 속성을 통해 셀의 배경색을 설정하여) 셀의 배경색을 변경하려면 데이터 원본의 View:ForCell이 아닌 대리인의 View:willDisplayCell:forRowAtIndexPath: 메서드에서 변경해야 합니다.그룹 스타일 테이블 보기에서 셀의 배경색을 변경하면 iOS 3.0에서 이전 버전의 운영 체제와 다른 영향을 받습니다.이제는 바깥쪽이 아닌 둥근 사각형 내부의 영역에 영향을 미칩니다."

https://developer.apple.com/documentation/uikit/uitableviewcell

배경색을 올바르게 설정하려면 다음 문서를 참조하십시오.

http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

그런 다음 다음 모든 행을 시도합니다.

[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];

UITableViewCell에 둘 이상의 보기가 숨어 있습니다.이것은 당신의 길을 가로막는 모든 것들을 명확하게 해줄 것입니다.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
}

간단한 해결책이 있습니다.

목표-c 버전:

cell.backgroundColor = [UIColor clearColor];

빠른 버전:

cell.backgroundColor = UIColor.clearColor()

기본적으로 UITableViewCell의 배경은 흰색입니다.하고있다myCell.backgroundColor = [UIColor clearColor];UITableViewCell을 투명하게 만들지만 UITableViewCell의 컨테이너인 UITableView도 기본적으로 배경이 흰색이기 때문에 차이를 느끼지 못합니다.

UIView 배경을 보려면 셀과 테이블 배경을 투명하게 만들어야 합니다.

myTableView.backgroundColor = [UIColor clearColor];
myTableCell.backgroundColor = [UIColor clearColor];

마틴 마가키안

사용자 지정 테이블 보기 셀 배경 이미지가 흰색 레이블 배경과 겹쳐지는 문제가 발생했습니다. 모든 방법을 시도한 결과 WillAppear가 수행한 보기에서 배경을 선명한 색으로 설정했습니다.

      - (void)viewWillAppear:(BOOL)animated
    {
[super viewWillAppear:animated];
    self.tableView.backgroundColor = [UIColor clearColor]; // if this is not here, cell background image is covered with a white rectangle :S
    }

및 ForCellAtIndexPath 행에서 배경 이미지를 설정합니다.

 if(!cell) {
    cell = [[[UITableViewCell alloc] initWithFrame: ...
    UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]];
    [cell setBackgroundView:cellBG];
    [cellBG release];
}

스토리보드를 사용하는 경우 프로토타입 UITableViewCells에 대해 "Opaque"를 선택 취소해야 합니다.

3단계가 있습니다.

  1. cell.contentView.backgroundColor = UIColor.clear
  2. 도면 섹션의 특성 검사기에서 테이블 뷰 셀에 대한 불투명 확인란의 선택을 취소합니다.스토리보드에서 테이블 뷰 셀 프로토타입을 클릭하고 속성 검사기를 열면 확인할 수 있습니다.아래로 스크롤하면 찾을 수 있습니다.
  3. 2단계와 동일한 위치의 드롭다운에서 Background as Clear Color(백그라운드를 Clear Color로 선택)를 선택합니다.
  4. 앱 실행

Swift 5.1용 솔루션

let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
cell.backgroundColor = UIColor.clear

대부분의 머리카락을 뽑은 후 xCode 13 / Swift 5.1의 경우 매우 간단했습니다.변경해야 할 사항이 두 가지 있습니다.

섹션의 행 수에 다음을 추가합니다.

tableView.backgroundColor = UIColor.clear

그래서 이것은 다음과 같이 보입니다.

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.backgroundColor = UIColor.clear
        return 40
    }

그런 다음 셀ForRowAt에서 다음을 추가합니다.

cell ?backgroundColor = UIColor.clear

그래서 다음과 같이 보입니다.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell?.backgroundColor = UIColor.clear
    return cell!
}

바로 그거야.작동했을 때 제 눈을 믿을 수 없었습니다 :)

파티에 늦었지만 여기 있는 모든 답변과 다르기 때문에 제 연구 결과를 공유하겠습니다.2시간 동안 다양한 솔루션을 사용해 본 결과, 인터페이스 빌더에서 두 가지 속성만 조정하여 테이블 셀의 투명성을 확보할 수 있었습니다.

  1. 테이블 뷰 배경(속성 검사기의 보기 섹션에서 맨 아래에 가까운)을 색상 지우기로 설정합니다.
  2. 표 보기 셀 배경(보기 섹션에서 다시)을 색상 지우기로 설정합니다.

코드를 변경할 필요가 없었으며, 여전히 인터페이스 작성기를 사용하는 사용자의 추가적인 장점은 테이블 셀이 투명(흰색이 아닌) 배경으로 표시된다는 것입니다. 이는 셀의 내용 항목 색상이 흰색일 때 중요합니다.

The result

Swift 4.1의 솔루션은 다음과 같습니다.

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundView = UIImageView(image: "Image")
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    cell.backgroundColor = UIColor.clear
{

언급URL : https://stackoverflow.com/questions/1008246/how-to-create-a-uitableviewcell-with-a-transparent-background

반응형