0
我将此color slider并入我的应用程序中,并且我无法使其适合表格视图单元格,以便它在两个纵向中占据单元格的长度和风景。以下是两张截图,展示了我的纵向和横向应用。您可以看到颜色滑块不会延伸单元格的全部长度。这是我设置颜色滑块的代码。我想弄清楚如何对CGRect进行属性编码,以便颜色滑块动态填充单元格的长度。在人像将表格视图单元格中的颜色滑块设置为单元格的全长
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)
}
EDIT(再次) 从你的建议,我在下面更新我的代码。这在纵向方向很有效。颜色滑块占据颜色标签右侧的单元格的整个大小。但是,当我将手机旋转至横向时,颜色滑块仅填满屏幕的一半。下面是它的外观截图。
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) ])
}
感谢您的回答。我尝试了这个,但它正在崩溃我的应用程序。有道理,我应该使用自动布局来实现这一点。我会继续玩代码。 – chickenparm
我提供的代码是一个例子,它很难让您看到您的完整代码的代码。当它崩溃时,你会得到什么错误。你在使用故事板吗? – AlexanderKaran
我正在使用故事板。我刚刚使用我在您的建议中尝试的代码更新了我的问题。感谢您的帮助! – chickenparm