Nice programing

이미지를 사용하기 위해 UITableView의 배경 (tableview 스타일은 "그룹화")을 어떻게 설정할 수 있습니까?

nicepro 2020. 12. 27. 20:44
반응형

이미지를 사용하기 위해 UITableView의 배경 (tableview 스타일은 "그룹화")을 어떻게 설정할 수 있습니까?


이미지를 사용하기 위해 UITableView의 배경 (tableview 스타일은 "Grouped")을 어떻게 설정할 수 있습니까?


최신 버전의 SDK에서는 tableView.backgroundView투명하게 하려면 설정 해야합니다. 다음과 같이 시도해보세요.

tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;

우리는 그 평범한 배경에 대해 뭔가를해야합니다. PNG 이미지를 사용하여 UITableView 뒤에 표시합니다.

  1. PNG 이미지를 준비하십시오. 320x460 (앱에 상태 표시 줄이 표시되는 경우) 또는 320x480 (숨긴 경우)이어야합니다.
  2. XCode로 리소스 폴더로 드래그하고 프로젝트에 추가하십시오.
  3. UITableView가 포함 된 NIB 파일을 Interface Builder에로드합니다.
  4. 라이브러리 (도구> 라이브러리)를 열고 미디어 탭으로 전환 한 다음 이미지를보기로 드래그하고 새 UIImageView를 만듭니다.
  5. 속성을 사용하여 이미지를 이동하고 크기를 조정하여 X = 0, Y = 0, 너비 = 320, 높이 = 480이되도록합니다.
  6. UITableView 뒤에 UIImageView 배치 (레이아웃> 맨 뒤로 보내기)
  7. 저장, 구축 및 이동!

실망스럽게도 배경을 볼 수 없습니다. UITableView의 배경이 UIImageView를 보지 못하도록 차단하고 있습니다. 다음 세 가지 사항을 변경해야합니다.

  1. Attributes Inspector에서 UITableView의 "불투명"확인란이 선택 해제되어 있는지 확인하십시오!
  2. UITableView의 배경색을 투명하게 설정합니다.

    tableView.backgroundColor = [UIColor clearColor];

이것이 도움이되고 문제가 해결되기를 바랍니다. 그것은 나를 위해 일했으며 아직 UITableView의 배경 이미지를 표시하는 더 우아한 방법을 찾지 못했습니다.

UITableView에서 직접 배경 이미지를 설정하는 것과 비교할 때 내 솔루션의 장점은 테이블의 내용을 들여 쓸 수 있다는 것입니다. 나는 종종 화면 하단에 2 개 또는 3 개의 표 셀을 표시하기 위해이 작업을 수행하고 싶었습니다.


[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"whatever.png"]]];

tableView.backgroundView = nil; 충분합니다. 배경색을 Clear Color로 설정할 필요가 없습니다.


한 가지 방법은 테이블 뷰를 투명하게 만들고 (뷰의 배경을 0 % 불투명도로 설정) UITableView 뒤에 UIImageView를 배치하는 것입니다. 투명한 표와 표 셀은 불투명 한 표와 잘 작동하지 않습니다.


UI Builder에서 배경색에는 "기타"옵션이 있습니다. 색상 선택기가 나타납니다. 색상 선택기에는 불투명도 설정이 있습니다. COLOR의 Opacity를 0으로 설정하면 작동하며 성능에 대해 말할 수 없습니다.


내가 발견 한 것은 투명한 배경의 "일반"스타일 테이블을 사용한 다음 각 셀의 backgroundView를 둥근 모양을 시뮬레이트하는 이미지가있는 UIImageView로 설정하여 둥근 모서리 셀의 모양을 다시 만들어야한다는 것입니다. 즉, 상단, 하단 및 중간 셀에는 다른 배경 이미지가 필요합니다.

그러나 이것은 사용자가 셀을 탭하고 "강조 표시"될 때 발생하는 상황을 다루지 않습니다. 그러면 사각형으로 표시됩니다. 위조 된 tablecell 배경 이미지에 대해 강조 표시된 이미지를 설정하여이 문제를 해결할 수 있습니다. 흰색으로 강조 표시된 버전으로 자신 만의 공개 액세서리보기 (ImageView)를 만들 수도 있습니다. 그런 다음 내가 사용하는 것과 같은 셀을 만들 수 있습니다 (아래). 이 셀 중 하나를 할당 한 후 backgroundView 및 accessoryView를 내 UIImageViews로 설정합니다.

#import "ClearBackRoundedTableCell.h"


@implementation ClearBackRoundedTableCell

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier 
{
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}


-  (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if( [[self.accessoryView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.accessoryView).highlighted = highlighted;

    if( [[self.backgroundView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.backgroundView).highlighted = highlighted;

    self.textLabel.highlighted = highlighted;
}

@end

이 경로로 이동하는 경우 한 가지 참고 : 그룹화 된 테이블의 셀 너비는 일반적으로 300px (세로 모드)이지만 여기의 일반 테이블은 테이블의 각 측면에 회색 선을 허용하기 위해 302 너비 여야합니다. 일반적으로 테이블 셀의 "내용"외부에 있습니다.


After spending a while with color picker, I found out that you need to specify opaque background not for the table view cell xib, but for the Table View where the cells will be located, which is another xib. From what I have seen, table view cell background attributes have no visual effect.


try this one

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

It worked for me in grouped tableview.


Make UITableview background as clear color.


Programmatically you can do it like this if your image is added into your resources:

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
UIImage *backroundImage = [UIImage imageNamed:@"my_backround"];
UIImageView *backroundImageView = [[UIImageView alloc] initWithImage:backroundImage];

Else you can do it in Interface Builder with this style :

IB tableview with image behind

You may need to configure the header files interface from UITableViewController to UIViewController and add <UITableViewDataSource,UITableViewDelegate> ,also don't forget to set the attributes of the tableview to not be opaque and reconnect the tableviews datasource and delegate outlets to the viewcontroller.

ReferenceURL : https://stackoverflow.com/questions/894875/how-can-i-set-the-background-of-uitableview-the-tableview-style-is-grouped-t

반응형