ios - 带有 UITableViewCellAccessoryDisclosureIndicator 的 UITableViewCell 删除右边距

标签 ios uitableview ios8 autolayout

我使用自动布局UITableViewCell来实现iOS 8中引入的动态单元格高度。 我设置了单元格并将 accessoryType 设置为 UITableViewCellAccessoryDisclosureIndicator。我以编程方式进行所有布局。

我尝试这样做: self.layoutMargin = UIEdgeInsetsZero;UITableViewCell 内的 init 方法

enter image description here

我想删除右边距或通过 contentView 调整大小设置自定义值

最佳答案

编辑:添加了用于管理 textLabel 和 detailTextLabel 框架的代码。

您可以通过覆盖自定义单元格类中的 layoutSubViews 方法来实现这一点(如果您不使用一个,则先创建一个并在您的 TableView 中使用它)。将以下代码添加到您的 TableView 单元格类 .m 文件中:

const int ACCESORY_MARGIN = -10;
const int LABEL_MARGIN = -10;

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame;
    frame = self.textLabel.frame;
    frame.origin.x += LABEL_MARGIN;
    frame.size.width -= 2 * LABEL_MARGIN;
    self.textLabel.frame = frame;

    frame = self.detailTextLabel.frame;
    frame.origin.x += LABEL_MARGIN;
    frame.size.width -= 2 * LABEL_MARGIN;
    self.detailTextLabel.frame = frame;

    if (self.accessoryType != UITableViewCellAccessoryNone)
    {
        float estimatedAccesoryX = MAX(self.textLabel.frame.origin.x + self.textLabel.frame.size.width, self.detailTextLabel.frame.origin.x + self.detailTextLabel.frame.size.width);

        for (UIView *subview in self.subviews) {
            if (subview != self.textLabel &&
                subview != self.detailTextLabel &&
                subview != self.backgroundView &&
                subview != self.contentView &&
                subview != self.selectedBackgroundView &&
                subview != self.imageView &&
                subview.frame.origin.x > estimatedAccesoryX) {
                frame = subview.frame;
                frame.origin.x -= ACCESORY_MARGIN;
                subview.frame = frame;
                break;
            }
        }
    }
}

更改上面定义的常量以满足您的需要。

希望这有助于解决您的问题。谢谢。

关于ios - 带有 UITableViewCellAccessoryDisclosureIndicator 的 UITableViewCell 删除右边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850852/

相关文章:

ios8 - 模态视图 Controller 被解除后iOS 8键盘解除延迟

swift - 为 UIButton 添加模糊效果

ios - 从 Parse Swift 添加对象后 TableView 不显示数组

ios - 带有自定义动画的 UINavigationViewControllerDelegate 会破坏默认行为吗?

ios - 如何隐藏 UITableView 中的空行并根据非空行更改 Uitableview 的高度

ios - UITableView滑动编辑

ios - swift 中的 XML + 模式

ios - UIImagePickerController 错误 : Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7

ios - 如何在旋转后更改自定义 viewForHeaderInSection 宽度

ios - Cocoapods 同一个 pod 文件中的多个库版本