2014-04-27 33 views
1

我有一個包含UIView的UITableViewCell。當單元格是該部分中唯一的行(即未在我的實現中選中)時,我想將UIView的所有角落四捨五入。如果單元格被選中,那麼我只想繞過UIView的頂部兩個角落。我試圖在自定義tableviewcell的layoutSubviews中做到這一點。如何處理旋轉的UIView圓角(使用CAShapeLayer)

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    self.roundedView.layer.mask = nil; 

    if (self.sectionSelected) { 
     self.maskLayer = [CAShapeLayer layer]; 
     self.maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.roundedView.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(10, 10)].CGPath; 
     self.roundedView.layer.mask = self.maskLayer; 
    } else { 
     self.maskLayer = [CAShapeLayer layer]; 
     self.maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.roundedView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)].CGPath; 
     self.roundedView.layer.mask = self.maskLayer; 
    } 
} 

不幸的是,這種方法並不是一直工作。如果我有一個縱向選中的單元格(self.sectionSelected = YES)。然後取消選擇(self.sectionSelected = NO)。然後旋轉到橫向並再次選擇單元格。它顯示單元的大小,它假設是縱向的。所以我相信它仍然應用肖像圖層蒙版,而不是風景蒙版。如果我將它從視圖中滾出並返回,它會更新爲適當的寬度。另外值得注意的是,我調用[self.tableView reloadData];在didRotateFromInterfaceOrientation中。

避免此問題的任何想法?在此先感謝

+0

進一步挖掘它。看起來,roundedView.bounds首先返回縱向視圖的大小而不是橫向。爲什麼會發生這種情況? – Stephen

+0

如果您使用tableView自己的選擇邏輯,可能值得在'setSelected:animated:'中執行此操作 – Nick

回答

1

解決方法是簡單地添加「self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;」