[英]iOS UITableView cell's custom separator doesn't change its width on landscape
我有UITableView並設置了單元格的分隔符。 一切似乎都很好,但我的分隔符沒有改變其在景觀上的寬度。 外觀如下:
這是我創建自定義單元格的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ListCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = UIColor.clearColor;
}
//set cell's image and texts
...
UIView *separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
separatorLineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.1];
[cell.contentView addSubview:separatorLineView];
[cell layoutIfNeeded];
return cell;
}
有什么想法,我該如何解決這個問題?
在ListCell.h-中添加分隔線屬性
@property (strong, nonatomic) UIView *separatorLineView;
嘗試用以下代碼替換分隔線的代碼-
int screenHeight;
if ( [[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationPortrait
&& [[UIApplication sharedApplication ]statusBarOrientation] != UIInterfaceOrientationPortraitUpsideDown){
screenHeight = [UIScreen mainScreen].applicationFrame.size.width;
} else {
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
}
if(cell.separatorLineView.superview == NULL){
cell.separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenHeight, 1)];
cell.separatorLineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.1];
[cell.contentView addSubview:cell.separatorLineView];
[cell layoutIfNeeded];
}
我一定會工作!
或者,您可以按故事板添加分隔線-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.