如何调整从消失的景观和prevent定制的UITableView分离器 [英] How to resize custom UITableView separators on landscape and prevent from disappearing

查看:133
本文介绍了如何调整从消失的景观和prevent定制的UITableView分离器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定以编程方式创建自己的的UITableView 分割线,因为我需要在上面的显示和/或低于每个分隔精细控制的UITableViewCell 。我的的tableView 有静态的细胞,所以不创建的cellForRowAtIndexPath 分隔符。相反,我有属性 S代表每个单元,并根据需要在 viewDidLoad中,我添加了一个顶部和/或底部分离器。它的工作,直到我旋转为横向,然后分隔线不拉伸以填充屏幕 - 这当然是相同的宽度创建时它是。我没有看到我怎么能自动调整以适合屏幕的宽度。

我尝试添加自动布局约束(前置,顶部/底部),但由于某种原因它不工作 - 宽度没有变化,但没有错误消息记录,表明什么是错的制约。该分割线有时也消失在滚动或旋转,如果我注释掉汽车布局限制那么他们就不会消失。

那么,如何才能让我的自定义单元格分隔符总是延长,以填补在旋转设备宽度,以及如何从消失prevent他们?

如果它会更容易/更好地以不同的方式来创建我的自定义细胞分离机,我愿意这样做。我只是不知道这可怎么除了我的做法,当细胞是静态完成。我考虑创造故事板视图,和视觉建立的约束,但也并非是我在做什么编程等效?如果他们是动态的细胞,我会做它的cellForRowAtIndexPath

  //在viewDidLoad中:
[个体经营addTopSeparatorForCell:self.myCell];// Helper方法
- (无效)addTopSeparatorForCell:(*的UITableViewCell){细胞
    UIView的* topSeparator = [[UIView的页头] initWithFrame:方法CGRectMake(15,1,cell.contentView.frame.size.width,0.5)];    //添加到CALayer的preserve行分隔符时的可视性行突出
    CALayer的* backgroundColorLayer = [CALayer的层]
    backgroundColorLayer.frame = topSeparator.bounds;
    backgroundColorLayer.backgroundColor = [的UIColor colorWithWhite:204 / 255.0f阿尔法:1] .CGColor;    [topSeparator.layer addSublayer:backgroundColorLayer];    [cell.contentView addSubview:topSeparator];    //添加自动布局限制
    topSeparator.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint * CN =零;
    CN = [NSLayoutConstraint constraintWithItem:topSeparator
                                      属性:NSLayoutAttributeLeading
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      属性:NSLayoutAttributeLeading
                                     事半功倍:1.0
                                       恒:15];
    [cell.contentView addConstraint:CN]
    CN = [NSLayoutConstraint constraintWithItem:topSeparator
                                      属性:NSLayoutAttributeTrailing
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      属性:NSLayoutAttributeTrailing
                                     事半功倍:1.0
                                       常数:0];
    [cell.contentView addConstraint:CN]
    CN = [NSLayoutConstraint constraintWithItem:topSeparator
                                      属性:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      属性:NSLayoutAttributeTop
                                     事半功倍:1.0
                                       常数:1];
    [cell.contentView addConstraint:CN]
}

修改:感谢@ user1966109,我们已经能够与线不延伸到填充的宽度来解决这个问题,现在他们突出一个小区时是preserved。但是,有一个问题仍然存在,我一直没能解决的,因为我不知道为什么它的发生。在分割线向下滚动滚动备份后消失。虽然因为其中有其他问题previous解决方案并没有表现出这种问题它涉及到汽车布局约束。下面是导致该行消失,当前的解决方案。我倒是AP preciate,如果有人知道如何prevent这个问题(preserve已经解决了其他问题)。

  [self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@H:|  - (15 @ 750) -  [MyView的]  - ( -  47 @ 750) -  |选项​​:0的指标:0浏览:viewsDictionary]];
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:[MyView的(2)] - ( - 2 @ 750) - |选项​​:0的指标:0浏览:viewsDictionary]];


解决方案

通过user1966109的初始帮助,我已经想通了,解决所有的问题和运作良好的约束:

  [单元addConstraint:[NSLayoutConstraint constraintWithItem:ImageView的
                                                     属性:NSLayoutAttributeLeading
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:细胞
                                                     属性:NSLayoutAttributeLeading
                                                    事半功倍:1.0
                                                      恒:缩进]];
    [单元addConstraint:[NSLayoutConstraint constraintWithItem:ImageView的
                                                     属性:NSLayoutAttributeTrailing
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:细胞
                                                     属性:NSLayoutAttributeTrailing
                                                    事半功倍:1.0
                                                      恒:0.0];
    [单元addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [ImageView的(0.5)选项:0的指标:0浏览:viewsDictionary]];

I've decided to programmatically create my own UITableView separator lines because I need fine control over displaying a separator above and/or below each individual UITableViewCell. My tableView has static cells, so I do not create the separators in cellForRowAtIndexPath. Instead, I have propertys for each cell and in viewDidLoad, I add a top and/or bottom separator as needed. It's working, until I rotate to landscape and then the separator line does not stretch to fill the screen - it of course remains the same width it was when created. I'm not seeing how I can automatically adjust them to fit the width of the screen.

I tried adding Auto Layout constraints (leading, trailing, top/bottom), but for some reason it's not working - the width does not change, but there are no error messages logged to indicate anything is wrong with the constraints. The separator lines also sometimes disappear upon scroll or rotate, and if I comment out the auto layout constraints then they do not disappear.

So how can I make my custom cell separators always stretch to fill the device width upon rotation, and how do I prevent them from disappearing?

If it would be easier/better to create my custom cell separators in a different way, I am willing to do that. I just don't know how this can be done aside from my approach when the cells are static. I considered creating the views in the Storyboard, and setting up the constraints visually, but would that not be the equivalent of what I'm doing programmatically? If they were dynamic cells I would do it in cellForRowAtIndexPath.

//In viewDidLoad:
[self addTopSeparatorForCell:self.myCell];

//Helper method
- (void)addTopSeparatorForCell:(UITableViewCell *)cell {
    UIView *topSeparator = [[UIView alloc] initWithFrame:CGRectMake(15, 1, cell.contentView.frame.size.width, 0.5)];

    //add CALayer to preserve line separator visibility when row is highlighted
    CALayer *backgroundColorLayer = [CALayer layer];
    backgroundColorLayer.frame = topSeparator.bounds;
    backgroundColorLayer.backgroundColor = [UIColor colorWithWhite:204/255.0f alpha:1].CGColor;

    [topSeparator.layer addSublayer:backgroundColorLayer];

    [cell.contentView addSubview:topSeparator];

    //add auto layout constraints
    topSeparator.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *cn = nil;
    cn = [NSLayoutConstraint constraintWithItem:topSeparator
                                      attribute:NSLayoutAttributeLeading
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      attribute:NSLayoutAttributeLeading
                                     multiplier:1.0
                                       constant:15];
    [cell.contentView addConstraint:cn];
    cn = [NSLayoutConstraint constraintWithItem:topSeparator
                                      attribute:NSLayoutAttributeTrailing
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      attribute:NSLayoutAttributeTrailing
                                     multiplier:1.0
                                       constant:0];
    [cell.contentView addConstraint:cn];
    cn = [NSLayoutConstraint constraintWithItem:topSeparator
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:cell.contentView
                                      attribute:NSLayoutAttributeTop
                                     multiplier:1.0
                                       constant:1];
    [cell.contentView addConstraint:cn];
}

EDIT: Thanks to @ user1966109, we've been able to solve the issue with the lines not extending to fill the width, and now they are preserved when highlighting a cell. But one issue still remains that I haven't been able to solve, since I'm not sure why it's occurring. The separator lines disappear after scrolling down the scrolling back up. It's related to the auto layout constraints though because a previous solution which had other issues did not exhibit this problem. Here's the current solution that causes the lines to disappear. I'd appreciate it if someone knows how to prevent this problem (and preserve the other issues already resolved).

[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(15@750)-[myView]-(-47@750)-|" options:0 metrics:0 views:viewsDictionary]];
[self.cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(2)]-(-2@750)-|" options:0 metrics:0 views:viewsDictionary]];

解决方案

With the initial help of user1966109, I have figured out constraints that address all of the problems and are working well:

    [cell addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                     attribute:NSLayoutAttributeLeading
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:cell
                                                     attribute:NSLayoutAttributeLeading
                                                    multiplier:1.0
                                                      constant:indent]];
    [cell addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                     attribute:NSLayoutAttributeTrailing
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:cell
                                                     attribute:NSLayoutAttributeTrailing
                                                    multiplier:1.0
                                                      constant:0.0]];
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(0.5)]" options:0 metrics:0 views:viewsDictionary]];

这篇关于如何调整从消失的景观和prevent定制的UITableView分离器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆