升級到XCode6後,iOS8裏設置tableview的setSeparatorInset:UIEdgeInsetsZero不起做用的解決辦法

首先咱們拖拽一個默認的tableview 控件! 看下xcode5 面板的inspector(檢查器)ios

咱們能夠找到一個 Separator Insetss 標籤 默認是 Defaultxcode

咱們選擇一下 發現有個Custom  這時候咱們驚奇的發現Left  15  ,這時候咱們只要把這個 15  改爲 0 , 而後保存, 你就會發現tableview 的分割線跟之前同樣了。atom

 

有些朋友問了若是是代碼寫的tableview 的呢。spa

下面咱們接着分析這個問題,讓咱們查詢下 tableview delegate  咱們會發現 ios7 增長了一些新屬性,code

@property (nonatomic)         UIEdgeInsets                separatorInset       NS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;// allows customization of the frame of cell separatorsci

 

這個時候你應改發現separatorInset 這個單詞是否有點眼熟,  蘋果公司已經給了註釋,能夠自定義視cell 的分割線,it

 

UIEdgeInsets 是個結構體類型,這時候咱們發現了咱們要的屬性 left
io

typedef struct UIEdgeInsets {table

    CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'class

} UIEdgeInsets;

 

咱們在使用tableview時會發現分割線的左邊會短一些,一般能夠使用 setSeparatorInset:UIEdgeInsetsZero 來解決。可是升級到XCode6以後,在iOS8裏發現沒有效果。下面給出解決辦法:

首先在viewDidLoad方法中加上以下代碼:

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

[self.tableView setSeparatorInset:UIEdgeInsetsZero];

}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

[self.tableView setLayoutMargins:UIEdgeInsetsZero];

}

而後在willDisplayCell方法中加入以下代碼:

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

{

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

[cell setSeparatorInset:UIEdgeInsetsZero];

}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

}

這樣就能夠正常顯示了。

相關文章
相關標籤/搜索