ios - 将表格 View 单元格中的颜色 slider 设置为单元格的全长

标签 ios swift

我正在合并这个 color slider进入我的应用程序,但我无法将其放入表格 View 单元格中,因此它在纵向和横向 View 中都占据了单元格的长度。下面是两个屏幕截图,以纵向和横向显示我的应用程序。您可以看到颜色 slider 没有延伸整个单元格的长度。这是我设置颜色 slider 的代码。我试图弄清楚如何对 CGRect 进行属性编码,以便颜色 slider 动态填充单元格的长度。

  func configureColorSlider() {
    let colorSlider = ColorSlider()
    let xCell = colorCell.contentView.bounds.width
    let yCell = colorCell.contentView.bounds.height
    colorSlider.frame = CGRect(x: xCell / 4, y: yCell / 4, width: 200, height: 24)
    colorSlider.orientation = .horizontal
    colorSlider.addTarget(self, action: #selector(ConfigureTimestampTableViewController.changedColor(_:)), for: .valueChanged)
    colorCell.contentView.addSubview(colorSlider)
  }

当前纵向 View : Color Slider Portrait

当前横向 View : Color Slide Landscape

编辑(再次) 根据您的建议,我在下面更新了我的代码。这在纵向方向上非常有效。颜色 slider 占据颜色标签右侧单元格的完整大小。但是,当我将手机旋转到横向时,颜色 slider 仅占满屏幕的一半。下面是它的外观截图。

  func configureColorSlider() {
    let colorSlider = ColorSlider()
    let xCell = colorCell.contentView.bounds.width
    let yCell = colorCell.contentView.bounds.height
    colorSlider.frame = CGRect(x: xCell / 4, y: yCell / 4, width: 200, height: 24)
    colorSlider.orientation = .horizontal
    colorSlider.addTarget(self, action: #selector(ConfigureTimestampTableViewController.changedColor(_:)), for: .valueChanged)

    colorCell.contentView.addSubview(colorSlider)

    colorSlider.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([colorSlider.leadingAnchor.constraint(equalTo: colorLabel.trailingAnchor, constant: 8),
                                 colorSlider.trailingAnchor.constraint(equalTo: colorCell.contentView.trailingAnchor, constant: -8),
                                 colorSlider.topAnchor.constraint(equalTo: colorCell.contentView.topAnchor, constant: 8),
                                 colorSlider.bottomAnchor.constraint(equalTo: colorCell.contentView.bottomAnchor, constant: -8) ])


}

Landscape Updated Code

最佳答案

原因是您已将彩色幻灯片的宽度设置为 200,因此它将始终为 200。

你应该使用自动布局来实现这一点,你可以在 Storyboard/nib 文件中添加约束,或者如果你以编程方式构建 View ,你可以使用 anchor 添加自动布局,例如像这样

colorSlider.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activateConstraints([
        colorSlider.leadingAnchor.constraintEqualToAnchor(UILable.leadingAnchor),
        colorSlider.trailingAnchor.constraintEqualToAnchor(cell.trailingAnchor),
        colorSlider.topAnchor.constraintEqualToAnchor(cell.topAnchor, constant: 8),

colorSlider.bottomAnchor.constraintEqualToAnchor(cell.bottomAnchor, 常数: -8) ])

关于ios - 将表格 View 单元格中的颜色 slider 设置为单元格的全长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40580016/

相关文章:

objective-c - iphone 中的语言翻译

ios - iOS UIDocumentBrowserViewController获取所选文件的URL路径

ios - 在从初始化器 swift 返回之前,在所有路径上调用 'Super.init isn' t 时出现问题。

ios - UItextView 委托(delegate) swift - textViewDidBeginEditing 未调用

ios - UIScrollView 按钮点击不起作用

ios - 如何根据滚动动态增加UITableview的高度

iphone - UIView 内的 UIButton 不可点击

ios - Google Analytics 如何计算请求/点击频率?

ios - 如何从另一个 uitableviewcell 中的 tableview 导航?

ios - 通过在本地更新数组模型来重新加载特定的表格 View 单元