LoginSignup
10
10

More than 5 years have passed since last update.

UITableViewとUITableViewCellの背景を透明にする

Posted at

なぜかXIBでUITableViewとUITableViewCellの背景色を半透明にするだけでは、セルを半透明にできなかったので備忘録として。

UITableViewを半透明にする.

-(void)viewDidLoad {
  // 半透明にする.
  UIColor* tableBackgroundColor = [UIColor blackColor];
  tableBackgroundColor = [tableBackgroundColor colorWithAlphaComponent:0.5];

  [self.tableView setBackgroundColor:tableBackgroundColor];
}

UITableViewCellを透明にする.


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableCellTableViewCell* cell = (MyTableCellTableViewCell*)
        [tableView dequeueReusableCellWithIdentifier:MY_TABLE_CELL];
    if( cell == nil ) {
        cell = [self createCustomCell];
        // なぜかXIB上でclearColorを設定していも、セルが透明にならず、
        //  ここで設定する事でセルが透明になった。
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    return cell;
}

// テーブルセルを生成する
-(MyTableCell*)createCustomCell {
    MyTableCell* result = nil;
    NSArray* nibArray = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:nil options:nil];
    for(id obj in nibArray) {
        if( [obj isMemberOfClass:[MyTableCell class]] ) {
            result = (MyTableCell*)obj;
            break;
        }
    }
    return result;
}

参考

UITableViewの背景を透過色にする

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10