2016-11-13 58 views
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) 
    } 

当前观点:在景观 Color Slider Portrait

当前观点: Color Slide Landscape

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) ]) 


} 

Landscape Updated Code

回答

1

原因是你已经设置了彩色幻灯片200的宽度,所以它永远是200

你应该使用自动布局来实现这一目标,你可以添加约束在情节提要/笔尖文件或你,如果你建立视图programticly您可以添加自动布局使用anchores例如像这样

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,constant:-8) ])

+0

感谢您的回答。我尝试了这个,但它正在崩溃我的应用程序。有道理,我应该使用自动布局来实现这一点。我会继续玩代码。 – chickenparm

+0

我提供的代码是一个例子,它很难让您看到您的完整代码的代码。当它崩溃时,你会得到什么错误。你在使用故事板吗? – AlexanderKaran

+0

我正在使用故事板。我刚刚使用我在您的建议中尝试的代码更新了我的问题。感谢您的帮助! – chickenparm

相关问题