1
votes

I'm new to objective C and I can't seem to set the correct UIView in a tableViewCell when rotating BACK to portrait after first rotating to landscape.

I checked several related posts, but nothing seems to solves my problem. When I print the width I get the expected results although the output is incorrect.

I created a UIView "lineView" to add custom lines between the cells in my tableView (this was necessary due to some seperator problems in different sections).

Here's part of the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];    

     ... Content of the cell

     UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 42,tableView.bounds.size.width-20, 0.7);    
     lineView.backgroundColor = [UIColor blackColor];

     [cell.contentView addSubview:lineView];

     return cell;
}

When I run my whole program I get the following outpus (from left to right: no rotation, rotate to landscape, rotate back to portrait):

enter image description here enter image description hereenter image description here

The seperator lines in figure 1 and 2 are correct, but when I rotate back to landscape the right inset dissapears. There probably is a trivial solution but my primitive skills are letting me down.

What am I missing?

1
Is it an option applying constraints to that separator UIView? I mean eg.: [lineView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lineView(==0.7]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myView)]]; And the similar for the width too.nzs
Thank you! It was definitely an option. I tried contraintWithVisualFormat as well as constraintsWithItem. Both ways work after a few adaptations in the code. I will add the code in the answer.baklan

1 Answers

0
votes

Here's how I fixed my problem (thanks to @codedad's comment) in case anyone ever runs in to a similar problem:

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];    

     ... Content of the cell

    UIView *lineView = [[UIView alloc] init];
    lineView.translatesAutoresizingMaskIntoConstraints=NO;

    [cell.contentView addSubview:lineView];

    NSArray *verticalConstraints =
      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-41.5-[lineView(0.8)]"
                                            options: 0
                                            metrics:nil
                                            views:NSDictionaryOfVariableBindings(lineView)];


    NSArray *horizontalConstraints =
      [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[lineView(>=50)]-20-|"
                                        options: 0
                                        metrics:nil
                                          views:NSDictionaryOfVariableBindings(lineView)];

    [cell.contentView addConstraints:verticalConstraints];
    [cell.contentView addConstraints:horizontalConstraints];
}

You can also you use 'constraintsWithItem'. I tried both approaches and they both worked fine (https://developer.apple.com/library/prerelease/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html) .

I still didn't figure out why working with 'frame' caused the initial problem but working with NSLayoutconstrainClass seems a safer and more solid approach anyway.