简体   繁体   中英

iOS UITableView cell's custom separator doesn't change its width on landscape

I have my UITableView and have set my cell's separator. Everything seems fine, but my separator doesn't change its width on landscape. Here is how it looks: 在此处输入图片说明

Here is the code where I create my custom cell:

- (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;
}

Any ideas, how can I solve this problem?

Add separator line property in ListCell.h-

@property (strong, nonatomic)  UIView *separatorLineView;

Try to replace separator line's code with following code -

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];
}

I will definitely work!

Or you can add separator line by storyboard-

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM