【问题标题】:UITableViewCell content overlaps delete button when in editing mode in iOS7在 iOS7 中处于编辑模式时,UITableViewCell 内容与删除按钮重叠
【发布时间】:2013-09-25 11:14:09
【问题描述】:

我正在使用自定义UITableViewCells 创建一个UITableView。 iOS 7 的新删除按钮导致我的单元格布局出现一些问题。

如果我使用“编辑”按钮,这会使红色圆圈出现,我会遇到问题,但如果我滑动单个单元格,它看起来很完美。

这是使用编辑按钮的时候:

[self.tableView setEditing:!self.tableView.editing animated:YES];

这是我滑动单个单元格的时候:

如您所见,我的标签与第一个示例中的删除按钮重叠。为什么会这样,我该如何解决?

【问题讨论】:

标签: objective-c uitableview ios7


【解决方案1】:

尝试使用UITableViewCellaccessoryVieweditingAccessoryView 属性,而不是自己添加视图。

如果您希望在编辑模式和非编辑模式下都显示相同的指示器,请尝试将两个视图属性设置为指向 uiTableViewCell 中的同一视图,例如:

self.accessoryView = self.imgPushEnabled;
self.editingAccessoryView = self.imgPushEnabled;

IOS7的表格编辑动画似乎有一个小故障,当切换回非编辑状态时,删除按钮和accessoryView重叠。当指定 accesoryView 并且 editingAccessoryViewnil 时,这似乎会发生。

这个故障的解决方法,似乎是指定一个不可见的editingAccessoryView,如:

self.editingAccessoryView =[[UIView alloc] init];
self.editingAccessoryView.backgroundColor = [UIColor clearColor];

【讨论】:

  • 我没有得到“如果你想要相同的指标”,这似乎并不能解决我的问题。
  • 但这并不是要显示相同的指标。这是关于单元格的布局。或者有删除指示器时删除按钮太大。
  • 确实如此。这是一个解决与 iOS7 中 UITableViewCells 布局方式相关的问题的解决方案。 accesoryViews 似乎在所有情况下都正确布局,所以这个答案建议使用它们,而不是自己向单元格添加视图。
【解决方案2】:

问题是在编辑模式下,单元格的 contentView 大小会发生变化。所以要么你必须在你的单元格中覆盖layoutSubviews 并支持不同的帧大小

- (void) layoutSubviews
{
    [super layoutSubviews];
    CGRect contentFrame = self.contentView.frame;
    // adjust to the contentView frame
    ...
}

或者你上钩并切换到自动布局。

首先我认为将contentView.clipsToBounds 设置为YES 可能是一个丑陋的解决方法,但似乎不起作用。

【讨论】:

  • 我使用的是 self.bounds(不会改变)而不是 self.contentView.frame。谢谢!
【解决方案3】:

我已经通过设置没有宽度的约束解决了这个问题,只有前导和尾随

【讨论】:

  • 你能具体点吗,我在iOS7中也有同样的问题
  • 我有下一个视图堆栈:cell - contentView - backgroundImageView;
  • 我有下一个视图堆栈:cell - contentView - backgroundImageView;当我为 backgroundImageView 设置约束时,例如:leading = 0;顶部 = 0;高度 = 一些高度;宽度=一些宽度;然后 deleteButton 显示在 backgroundImageView 下。我已将约束宽度更改为尾随。 BackgroundImageView 开始改变大小,deleteButton 完全显示
  • 这也解决了我的问题。添加正确的约束允许单元格的 contenView 在编辑模式下正确地重新定位它。
  • @MishaVyrko,嗨 Misha,我也在为同样的问题苦苦挣扎。为 iphone 5 编译时,删除按钮是不可见的。对于第 6 部 iPhone,我可以看到它。我完全是关于约束的新手,我在约束菜单中找不到 backgroundImageView 属性。我有最新的 xCode。你能帮忙吗
【解决方案4】:

正如 tcurdt 所提到的,您可以切换到自动布局来解决这个问题。但是,如果您(可以理解)不想仅仅为这个实例弄乱自动布局,您可以设置 autoresizingMask 并将其自动转换为适当的自动布局约束。

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

【讨论】:

    【解决方案5】:

    只要在你自定义的 TableViewCell 类中使用这个方法就可以得到完美的答案,

    这里selfUITableviewCell

    - (void)layoutSubviews {
    
    [super layoutSubviews];
    
        for (UIView *subview in self.subviews) {
    
        for (UIView *subview2 in subview.subviews) {
    
            if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
    
                [subview bringSubviewToFront:subview2];
    
            }
         }
      }
    }
    

    【讨论】:

      【解决方案6】:

      如果有人想调整删除按钮大小,请使用以下代码

      - (void)layoutSubviews {
      
          [super layoutSubviews];
      
          for (UIView *subview in self.subviews) {
      
              for (UIView *subview2 in subview.subviews) {
      
                  if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
      
                      CGRect rect = subview2.frame;
                      rect.size.height = 47; //adjusting the view height
                      subview2.frame = rect;
      
                      for (UIButton *btn in [subview2 subviews]) {
      
                          if ([NSStringFromClass([btn class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) { // adjusting the Button height
                              rect = btn.frame;
                              rect.size.height = CGRectGetHeight(subview2.frame);
                              btn.frame = rect;
      
                              break;
                          }
                      }
      
                      [subview bringSubviewToFront:subview2];
      
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案7】:

        消除此问题的最佳方法是在单元格中添加图像并将其设置在 Backside。

              UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImg.png"]];
              imageView.frame = CGRectMake(0, 0, 320, yourCustomCell.frame.size.height);
              [yourCustomCell addSubview:imageView];
              [yourCustomCell sendSubviewToBack:imageView];
        

        如果您的文本会与删除按钮重叠,请实施自动布局。它会以更好的方式管理它。

        可以生成另一种情况,即 cellSelectionStyle 将使用默认颜色突出显示。您可以按如下方式设置高亮颜色

              yourCustomCell.selectionStyle = UITableViewCellSelectionStyleNone;
        

        将表格单元格的选择样式设置为 UITableViewCellSelectionStyleNone。这将删除蓝色背景突出显示或其他。然后,要使文本标签或内容视图突出显示以您想要的方式工作,请在您的CustomCell.m 类中使用此方法。

        - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
        {
            if (highlighted)
                self.contentView.backgroundColor = [UIColor greenColor];
             else
                self.contentView.backgroundColor = [UIColor clearColor];
        }
        

        我希望你能更好地理解它。

        【讨论】:

        • 这有什么帮助!?
        • 嗨@tcurdt,如果您在单元格的背景添加图像,然后通过 sendSubviewToBack 方法设置它,它将停止重叠。如果您在 customCell 类中应用 setHighlighted 方法,那么它将帮助您设置单元格的突出显示颜色。
        • 添加颜色有什么关系?这与突出显示颜色无关。为什么要在单元格的背景中添加图像来修复布局(或滚动单元格的滚动视图)。抱歉 - 还是不明白。
        【解决方案8】:

        将自定义单元格的layoutSubviews 中的UITableViewCellDeleteConfirmationView 放在前面对我来说适用于iPhone,但不适用于iPad。

        我在 iPad 的 splitViewController 的主要部分有一个 UITableView,在这种情况下 UITableViewCellDeleteConfirmationView 的框架是 (768 0; 89 44),而不是 (320 0; 89 44)

        所以我在 layoutSubviews 方法中调整了框架的大小,这对我有用

        - (void)layoutSubviews {
            [super layoutSubviews];
            for (UIView *subview in self.subviews) 
            {
                for (UIView *subview2 in subview.subviews) 
                {
                    if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) 
                    { 
                        CGRect frame = subview2.frame;
                        frame.origin.x = 320;
                        subview2.frame = frame;
                        [subview bringSubviewToFront:subview2];
                   }
                }
            }
        }
        

        【讨论】:

          【解决方案9】:

          如果您将内容放在 UITableViewCell 的 contentView 中,请确保在 layoutSubviews 中使用 self.contentView.frame.size.width 而不是 self.frame.size.width

          self.frame在编辑模式下会扩展宽度,会导致右边的任何内容超出contentView的边界。 self.contentView.frame 保持正确的宽度(并且是您应该使用的)。

          【讨论】:

            【解决方案10】:

            试试这个:可能是你在 cellForRowAtIndexPath (委托方法)中设置单元格 setBackgroundImage。不要在这里设置。将您的图像设置在:

            -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cellList.png"]]; }
            

            享受编码。

            【讨论】:

              【解决方案11】:

              我的解决方案是在显示删除按钮时将整个 contentView 向左移动:

              override func layoutSubviews() {
                  super.layoutSubviews()
              
                  if editingStyle == UITableViewCellEditingStyle.Delete {
                      var rect = contentView.frame
                      rect.origin.x = self.showingDeleteConfirmation ? -15 : 38
                      contentView.frame = rect
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 2018-04-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-05-13
                • 1970-01-01
                • 2014-04-11
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多