2017-01-03 25 views
0

我使用xib文件構建了自定義tableview。我加了一個UILabel與頂部邊框,爲頂部邊框代碼如何在iOS中使用autoresizingMask在UILabel上應用頂部邊框

UIView *topBorder = [UIView new]; 
topBorder.backgroundColor = [UIColor blackColor]; 
topBorder.frame = CGRectMake(0, 0, cell.nameOfImage.frame.size.width, 1); 
[cell.nameOfImage addSubview:topBorder]; 

cell.nameOfImage.text = [tableData objectAtIndex:indexPath.row]; 

自動調整爲enter image description here

但它不能在屏幕的不同調整爲5秒和6秒加。它不顯示UILabel大小。在5S

enter image description here

而且在6S加 enter image description here

上邊框是不是相對於標籤顯示。

+0

試試這個https://stackoverflow.com/questions/7666863/ uiview-bottom-border/48109396#48109396 – Datt1994

回答

0

您需要根據cell.nameOfImage寬度更新寬度topBorder。移動你的代碼框架轉變爲layoutSubviews

Objective-C的

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    UIView *top = [[UIView alloc] init]; 
    topBorder.frame = CGRectMake(0.0, 0.0, nameOfImage.frame.size.width, 1.0) 
} 

斯威夫特

override func layoutSubviews() { 
     super.layoutSubviews() 
     topBorder.frame = CGRectMake(0, 0, nameOfImage.frame.size.width, 1); 
    } 
+0

是的,我需要根據單元格寬度的寄宿生 –

+0

你可以請客觀的c給我,我想我也加入了相同的t o幀 –

+0

您需要在xib文件中添加約束。選擇頂視圖,然後右拖圖像視圖。然後你選擇相等的寬度 – Irfan

相關問題